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.14 2003/07/12 17:41:45 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)
51 return (list_node_t *)xmalloc_and_zero(sizeof(list_node_t));
54 void list_free_node(list_t *list, list_node_t *node)
56 if(node->data && list->delete)
57 list->delete(node->data);
62 /* Insertion and deletion */
64 list_node_t *list_insert_head(list_t *list, void *data)
68 node = list_alloc_node();
72 node->next = list->head;
76 node->next->prev = node;
85 list_node_t *list_insert_tail(list_t *list, void *data)
89 node = list_alloc_node();
93 node->prev = list->tail;
97 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;
121 void list_delete_node(list_t *list, list_node_t *node)
123 list_unlink_node(list, node);
124 list_free_node(list, node);
127 void list_delete_head(list_t *list)
129 list_delete_node(list, list->head);
132 void list_delete_tail(list_t *list)
134 list_delete_node(list, list->tail);
137 /* Head/tail lookup */
139 void *list_get_head(list_t *list)
142 return list->head->data;
147 void *list_get_tail(list_t *list)
150 return list->tail->data;
155 /* Fast list deletion */
157 void list_delete_list(list_t *list)
159 list_node_t *node, *next;
161 for(node = list->head; node; node = next) {
163 list_free_node(list, node);
171 void list_foreach_node(list_t *list, list_action_node_t action)
173 list_node_t *node, *next;
175 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) {