2 list.c -- functions to deal with double linked lists
3 Copyright (C) 2000 Ivo Timmermans <itimmermans@bigfoot.com>
4 2000 Guus Sliepen <guus@sliepen.warande.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: list.c,v 1.1.2.3 2000/11/20 22:13:00 guus Exp $
38 Initialize a new list.
40 list_t *list_new(void)
44 list = xmalloc_and_zero(sizeof(list_t));
51 Delete the element pointed to by idx from the list.
53 list_node_t *list_delete(list_t *list, list_node_t *idx)
62 if(list->callbacks->delete != NULL)
63 if(list->callbacks->delete(idx->data))
64 syslog(LOG_WARNING, _("List callback[delete] failed for %08lx - freeing anyway"), idx->data);
69 /* First element in list */
72 list->head = idx->next;
75 /* Last element in list */
78 list->tail = idx->prev;
80 if(idx->prev != NULL && idx->next != NULL)
81 /* Neither first nor last element */
83 idx->prev->next = idx->next;
84 idx->next->prev = idx->prev;
86 if(list->head == NULL)
89 if(list->tail == NULL)
98 Call function() on each element in the list. If this function
99 returns non-zero, the element will be removed from the list.
101 void list_forall_nodes(list_t *list, int (*function)(void *data))
106 if(!list) /* no list given */
108 if(!function) /* no function given */
110 if(!list->head) /* list is empty */
112 for(p = list->head; p != NULL; p = p->next)
114 res = function(p->data);
116 p = list_delete(list, p);
123 Free all datastructures contained in this list. It uses the delete
124 callback for this list to free each element.
126 void list_destroy(list_t *list)
130 /* list_destroy_nodes(list); */
137 Append a new node to the list that points to data.
139 void list_append(list_t *list, void *data)
143 n = xmalloc_and_zero(sizeof(list_node_t));
145 n->prev = list->tail;
147 list->tail->next = n;