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.7 2001/01/07 15:24:52 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;
87 list_node_t *list_insert_tail(list_t *list, void *data)
91 node = list_alloc_node();
95 node->prev = list->tail;
99 node->prev->next = node;
106 void list_unlink_node(list_t *list, list_node_t *node)
109 node->prev->next = node->next;
111 list->head = node->next;
114 node->next->prev = node->prev;
116 list->tail = node->prev;
119 void list_delete_node(list_t *list, list_node_t *node)
121 list_unlink_node(list, node);
122 list_free_node(list, node);
125 void list_delete_head(list_t *list)
127 list_delete_node(list, list->head);
130 void list_delete_tail(list_t *list)
132 list_delete_node(list, list->tail);
135 /* Head/tail lookup */
137 void *list_get_head(list_t *list)
140 return list->head->data;
145 void *list_get_tail(list_t *list)
148 return list->tail->data;
153 /* Fast list deletion */
155 void list_delete_list(list_t *list)
157 list_node_t *node, *next;
159 for(node = list->head; node; node = next)
162 list_free_node(list, node);
170 void list_foreach_node(list_t *list, list_action_node_t action)
172 list_node_t *node, *next;
174 for(node = list->head; node; node = next)
181 void list_foreach(list_t *list, list_action_t action)
183 list_node_t *node, *next;
185 for(node = list->head; node; node = next)