]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: add listnode_add_head()
authorRenato Westphal <renato@opensourcerouting.org>
Mon, 28 May 2018 13:15:09 +0000 (10:15 -0300)
committerRenato Westphal <renato@opensourcerouting.org>
Mon, 13 Aug 2018 21:59:31 +0000 (18:59 -0300)
Provide a new convenience function that adds an element to the beginning
of a list.

Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
lib/linklist.c
lib/linklist.h

index 86649dd4952ec12dbdfec6c5272df9f8ce06ea8a..effd384e46d8971e643e74fa2029b34e87499572 100644 (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;
index cee6c1e505e54a4b565df32cec9bc49823760f71..f5cd44efb0f06318201ca48ffd98cbc21c2907db 100644 (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.
  *