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.
15 /* Add data to the start of the linked list. */
16 void FAST_FUNC llist_add_to(llist_t **old_head, void *data)
18 llist_t *new_head = xmalloc(sizeof(llist_t));
20 new_head->data = data;
21 new_head->link = *old_head;
25 /* Add data to the end of the linked list. */
26 void FAST_FUNC llist_add_to_end(llist_t **list_head, void *data)
28 llist_t *new_item = xmalloc(sizeof(llist_t));
30 new_item->data = data;
31 new_item->link = NULL;
34 *list_head = new_item;
36 llist_t *tail = *list_head;
40 tail->link = new_item;
44 /* Remove first element from the list and return it */
45 void* FAST_FUNC llist_pop(llist_t **head)
60 /* Unlink arbitrary given element from the list */
61 void FAST_FUNC llist_unlink(llist_t **head, llist_t *elm)
69 *head = (*head)->link;
73 for (crt = *head; crt; crt = crt->link) {
74 if (crt->link == elm) {
75 crt->link = elm->link;
81 /* Recursively free all elements in the linked list. If freeit != NULL
82 * call it on each datum in the list */
83 void FAST_FUNC llist_free(llist_t *elm, void (*freeit) (void *data))
86 void *data = llist_pop(&elm);
94 /* Reverse list order. */
95 llist_t* FAST_FUNC llist_rev(llist_t *list)
100 llist_t *next = list->link;