1 /* vi: set sw=4 ts=4: */
3 * linked list helper functions.
5 * Copyright (C) 2003 Glenn McGrath
6 * Copyright (C) 2005 Vladimir Oleynik
7 * Copyright (C) 2005 Bernhard Fischer
8 * Copyright (C) 2006 Rob Landley <rob@landley.net>
10 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
17 /* Add data to the start of the linked list. */
18 void llist_add_to(llist_t **old_head, void *data)
20 llist_t *new_head = xmalloc(sizeof(llist_t));
21 new_head->data = data;
22 new_head->link = *old_head;
27 #ifdef L_llist_add_to_end
28 /* Add data to the end of the linked list. */
29 void llist_add_to_end(llist_t **list_head, void *data)
31 llist_t *new_item = xmalloc(sizeof(llist_t));
32 new_item->data = data;
33 new_item->link = NULL;
35 if (!*list_head) *list_head = new_item;
37 llist_t *tail = *list_head;
38 while (tail->link) tail = tail->link;
39 tail->link = new_item;
45 /* Remove first element from the list and return it */
46 void *llist_pop(llist_t **head)
50 if(!*head) data = *head;
52 void *next = (*head)->link;
63 /* Recursively free all elements in the linked list. If freeit != NULL
64 * call it on each datum in the list */
65 void llist_free(llist_t *elm, void (*freeit)(void *data))
68 void *data = llist_pop(&elm);
69 if (freeit) freeit(data);