Skip to content

Commit 78e7b1f

Browse files
committed
Replace duplicated logic in list.h by new helpers
The traversal logic in list_pushback() and list_remove() is identical to the logic already provided by list_pushback_node() and list_remove_node(). This change replaces the duplicated code with calls to these helpers, reducing code duplication.
1 parent 6da5488 commit 78e7b1f

File tree

1 file changed

+3
-16
lines changed

1 file changed

+3
-16
lines changed

include/lib/list.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,9 @@ static inline list_node_t *list_pushback(list_t *list, void *data)
106106
return NULL;
107107

108108
node->data = data;
109-
node->next = list->tail;
109+
node->next = NULL;
110110

111-
/* Insert before tail sentinel */
112-
list_node_t *prev = list->head;
113-
while (prev->next != list->tail)
114-
prev = prev->next;
115-
prev->next = node;
116-
117-
list->length++;
111+
list_pushback_node(list, node);
118112
return node;
119113
}
120114

@@ -157,17 +151,10 @@ static inline void *list_remove(list_t *list, list_node_t *target)
157151
if (unlikely(!list || !target || list_is_empty(list)))
158152
return NULL;
159153

160-
list_node_t *prev = list->head;
161-
while (prev->next != list->tail && prev->next != target)
162-
prev = prev->next;
163-
164-
if (unlikely(prev->next != target))
165-
return NULL; /* node not found */
154+
list_remove_node(list, target);
166155

167-
prev->next = target->next;
168156
void *data = target->data;
169157
free(target);
170-
list->length--;
171158
return data;
172159
}
173160

0 commit comments

Comments
 (0)