2 list.c -- functions to deal with double linked lists
3 Copyright (C) 2000-2003 Ivo Timmermans <ivo@o2w.nl>
4 2000-2003 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.16 2003/07/17 15:06:25 guus Exp $
28 /* (De)constructors */
30 list_t *list_alloc(list_action_t delete)
34 list = xmalloc_and_zero(sizeof(list_t));
35 list->delete = delete;
40 void list_free(list_t *list)
45 list_node_t *list_alloc_node(void)
47 return (list_node_t *)xmalloc_and_zero(sizeof(list_node_t));
50 void list_free_node(list_t *list, list_node_t *node)
52 if(node->data && list->delete)
53 list->delete(node->data);
58 /* Insertion and deletion */
60 list_node_t *list_insert_head(list_t *list, void *data)
64 node = list_alloc_node();
68 node->next = list->head;
72 node->next->prev = node;
81 list_node_t *list_insert_tail(list_t *list, void *data)
85 node = list_alloc_node();
89 node->prev = list->tail;
93 node->prev->next = node;
102 void list_unlink_node(list_t *list, list_node_t *node)
105 node->prev->next = node->next;
107 list->head = node->next;
110 node->next->prev = node->prev;
112 list->tail = node->prev;
117 void list_delete_node(list_t *list, list_node_t *node)
119 list_unlink_node(list, node);
120 list_free_node(list, node);
123 void list_delete_head(list_t *list)
125 list_delete_node(list, list->head);
128 void list_delete_tail(list_t *list)
130 list_delete_node(list, list->tail);
133 /* Head/tail lookup */
135 void *list_get_head(list_t *list)
138 return list->head->data;
143 void *list_get_tail(list_t *list)
146 return list->tail->data;
151 /* Fast list deletion */
153 void list_delete_list(list_t *list)
155 list_node_t *node, *next;
157 for(node = list->head; node; node = next) {
159 list_free_node(list, node);
167 void list_foreach_node(list_t *list, list_action_node_t action)
169 list_node_t *node, *next;
171 for(node = list->head; node; node = next) {
177 void list_foreach(list_t *list, list_action_t action)
179 list_node_t *node, *next;
181 for(node = list->head; node; node = next) {