2 list.c -- functions to deal with double linked lists
3 Copyright (C) 2000,2001 Ivo Timmermans <ivo@o2w.nl>
4 2000,2001 Guus Sliepen <guus@sliepen.eu.org>
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.11 2002/06/21 10:11:11 guus Exp $
32 /* (De)constructors */
34 list_t *list_alloc(list_action_t delete)
38 list = xmalloc_and_zero(sizeof(list_t));
39 list->delete = delete;
44 void list_free(list_t *list)
49 list_node_t *list_alloc_node(void)
53 node = xmalloc_and_zero(sizeof(list_node_t));
58 void list_free_node(list_t *list, list_node_t *node)
60 if(node->data && list->delete)
61 list->delete(node->data);
66 /* Insertion and deletion */
68 list_node_t *list_insert_head(list_t *list, void *data)
72 node = list_alloc_node();
76 node->next = list->head;
80 node->next->prev = node;
89 list_node_t *list_insert_tail(list_t *list, void *data)
93 node = list_alloc_node();
97 node->prev = list->tail;
101 node->prev->next = node;
110 void list_unlink_node(list_t *list, list_node_t *node)
113 node->prev->next = node->next;
115 list->head = node->next;
118 node->next->prev = node->prev;
120 list->tail = node->prev;
125 void list_delete_node(list_t *list, list_node_t *node)
127 list_unlink_node(list, node);
128 list_free_node(list, node);
131 void list_delete_head(list_t *list)
133 list_delete_node(list, list->head);
136 void list_delete_tail(list_t *list)
138 list_delete_node(list, list->tail);
141 /* Head/tail lookup */
143 void *list_get_head(list_t *list)
146 return list->head->data;
151 void *list_get_tail(list_t *list)
154 return list->tail->data;
159 /* Fast list deletion */
161 void list_delete_list(list_t *list)
163 list_node_t *node, *next;
165 for(node = list->head; node; node = next)
168 list_free_node(list, node);
176 void list_foreach_node(list_t *list, list_action_node_t action)
178 list_node_t *node, *next;
180 for(node = list->head; node; node = next)
187 void list_foreach(list_t *list, list_action_t action)
189 list_node_t *node, *next;
191 for(node = list->head; node; node = next)