diff options
| author | Renato Westphal <renato@opensourcerouting.org> | 2018-05-28 10:15:09 -0300 |
|---|---|---|
| committer | Renato Westphal <renato@opensourcerouting.org> | 2018-08-13 18:59:31 -0300 |
| commit | 9ea82f28d499f2bf5668fde86474b3e5287e58c6 (patch) | |
| tree | 477b4f8d87db2932d745af59a69cba33d8a6e7f4 | |
| parent | 27982ebc9d211a9353029a043a120fdf291f5171 (diff) | |
lib: add listnode_add_head()
Provide a new convenience function that adds an element to the beginning
of a list.
Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
| -rw-r--r-- | lib/linklist.c | 20 | ||||
| -rw-r--r-- | lib/linklist.h | 13 |
2 files changed, 33 insertions, 0 deletions
diff --git a/lib/linklist.c b/lib/linklist.c index 86649dd495..effd384e46 100644 --- a/lib/linklist.c +++ b/lib/linklist.c @@ -70,6 +70,26 @@ void listnode_add(struct list *list, void *val) list->count++; } +void listnode_add_head(struct list *list, void *val) +{ + struct listnode *node; + + assert(val != NULL); + + node = listnode_new(); + + node->next = list->head; + node->data = val; + + if (list->head == NULL) + list->head = node; + else + list->head->prev = node; + list->head = node; + + list->count++; +} + void listnode_add_sort(struct list *list, void *val) { struct listnode *n; diff --git a/lib/linklist.h b/lib/linklist.h index cee6c1e505..f5cd44efb0 100644 --- a/lib/linklist.h +++ b/lib/linklist.h @@ -83,6 +83,19 @@ extern struct list *list_new(void); extern void listnode_add(struct list *list, void *data); /* + * Add a new element to the beginning of a list. + * + * Runtime is O(1). + * + * list + * list to operate on + * + * data + * element to add + */ +extern void listnode_add_head(struct list *list, void *data); + +/* * Insert a new element into a list with insertion sort. * * If list->cmp is set, this function is used to determine the position to |
