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 2000/10/20 16:44:32 zarq Exp $
36 Initialize a new list.
38 list_t *list_new(void)
42 list = xmalloc_and_zero(sizeof(list_t));
49 Delete the element pointed to by idx from the list.
51 list_node_t *list_delete(list_t *list, list_node_t *idx)
60 if(list->callbacks->delete != NULL)
61 if(list->callbacks->delete(idx->data))
62 error(ERR_WARNING, N_("List callback[delete] failed for %08lx - freeing anyway"), idx->data);
67 /* First element in list */
70 list->head = idx->next;
73 /* Last element in list */
76 list->tail = idx->prev;
78 if(idx->prev != NULL && idx->next != NULL)
79 /* Neither first nor last element */
81 idx->prev->next = idx->next;
82 idx->next->prev = idx->prev;
84 if(list->head == NULL)
87 if(list->tail == NULL)
96 Call function() on each element in the list. If this function
97 returns non-zero, the element will be removed from the list.
99 void list_forall_nodes(list_t *list, int (*function)(void *data))
104 if(!list) /* no list given */
106 if(!function) /* no function given */
108 if(!list->head) /* list is empty */
110 for(p = list->head; p != NULL; p = p->next)
112 res = function(p->data);
114 p = list_delete(list, p);
121 Free all datastructures contained in this list. It uses the delete
122 callback for this list to free each element.
124 void list_destroy(list_t *list)
128 list_destroy_nodes(list);
135 Append a new node to the list that points to data.
137 list_append(list_t *list, void *data)
141 n = xmalloc_and_zero(sizeof(list_node_t));
143 n->prev = list->tail;
144 list->tail->next = n;