Skip to content

Commit 7307a00

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 7307a00

File tree

1 file changed

+2
-14
lines changed

1 file changed

+2
-14
lines changed

include/lib/list.h

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,7 @@ static inline list_node_t *list_pushback(list_t *list, void *data)
108108
node->data = data;
109109
node->next = list->tail;
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,14 +151,8 @@ 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;
154+
list_remove_node(list, target);
163155

164-
if (unlikely(prev->next != target))
165-
return NULL; /* node not found */
166-
167-
prev->next = target->next;
168156
void *data = target->data;
169157
free(target);
170158
list->length--;

0 commit comments

Comments
 (0)