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.6 2000/11/22 23:09:38 guus Exp $
37 Initialize a new list.
39 list_t *list_new(void)
43 list = xmalloc_and_zero(sizeof(list_t));
50 Delete the element pointed to by idx from the list.
52 void list_delete(list_t *list, list_node_t *idx)
57 if(list->callbacks->delete != NULL)
58 if(list->callbacks->delete(idx->data))
59 syslog(LOG_WARNING, _("List callback[delete] failed for %08lx - freeing anyway"), idx->data);
64 /* First element in list */
66 list->head = idx->next;
69 /* Last element in list */
71 list->tail = idx->prev;
73 if(idx->prev != NULL && idx->next != NULL)
74 /* Neither first nor last element */
76 idx->prev->next = idx->next;
77 idx->next->prev = idx->prev;
79 if(list->head == NULL)
82 if(list->tail == NULL)
91 Call function() on each element in the list. If this function
92 returns non-zero, the element will be removed from the list.
94 void list_forall_nodes(list_t *list, int (*function)(void *data))
96 list_node_t *p, *next;
99 if(!list) /* no list given */
101 if(!function) /* no function given */
103 if(!list->head) /* list is empty */
105 for(p = list->head; p != NULL; p = next)
108 res = function(p->data);
110 list_delete(list, p);
117 Free all datastructures contained in this list. It uses the delete
118 callback for this list to free each element.
120 void list_destroy(list_t *list)
124 /* list_destroy_nodes(list); */
131 Append a new node to the list that points to data.
133 void list_append(list_t *list, void *data)
137 n = xmalloc_and_zero(sizeof(list_node_t));
139 n->prev = list->tail;
141 list->tail->next = n;