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>
This commit is contained in:
Renato Westphal 2018-05-28 10:15:09 -03:00
parent 27982ebc9d
commit 9ea82f28d4
2 changed files with 33 additions and 0 deletions

View File

@ -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;

View File

@ -82,6 +82,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.
*