fix typo
[arla.git] / util / list.c
blobf7c197b1014b6661f111263dc5c3727c550ba5a8
1 /*
2 * Copyright (c) 1995, 1996, 1997, 1998, 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * List handling functions
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 RCSID("$Id$");
41 #endif
43 #include <assert.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include "list.h"
49 * The representation is with a double-linked list, a pointer to
50 * the tail, and another one to the head.
54 * Create a new list.
57 List*
58 listnew (void)
60 List *tmp = (List *)malloc (sizeof (List));
62 if (tmp)
63 tmp->head = tmp->tail = NULL;
64 return tmp;
68 * Free a list, assume that its empty
71 void
72 listfree(List *list)
74 list->head = list->tail = NULL;
75 free(list);
79 * Add an element before `item'
82 Listitem *
83 listaddbefore (List *list, Listitem *old_item, void *data)
85 Listitem *item = (Listitem *)malloc (sizeof (Listitem));
87 if (item == NULL)
88 return item;
90 item->data = data;
91 item->prev = old_item->prev;
92 item->next = old_item;
93 if (item->prev)
94 item->prev->next = item;
95 else
96 list->head = item;
97 old_item->prev = item;
98 return item;
102 * Add an element after `item'
105 Listitem *
106 listaddafter (List *list, Listitem *old_item, void *data)
108 Listitem *item = (Listitem *)malloc (sizeof (Listitem));
110 if (item == NULL)
111 return item;
113 item->data = data;
114 item->next = old_item->next;
115 item->prev = old_item;
116 if (item->next)
117 item->next->prev = item;
118 else
119 list->tail = item;
120 old_item->next = item;
121 return item;
125 * Add an element to the head of the list
128 Listitem *
129 listaddhead (List *list, void *data)
131 Listitem *item = (Listitem *)malloc (sizeof (Listitem));
133 if (item == NULL)
134 return item;
136 item->data = data;
137 item->prev = NULL;
138 item->next = list->head;
139 if (list->head)
140 list->head->prev = item;
141 list->head = item;
142 if (list->tail == NULL)
143 list->tail = item;
144 return item;
148 * Add an element to the tail of the list
151 Listitem *
152 listaddtail (List *list, void *data)
154 Listitem *item = (Listitem *)malloc (sizeof (Listitem));
156 if (item == NULL)
157 return item;
159 item->data = data;
160 item->next = NULL;
161 item->prev = list->tail;
162 if (list->tail)
163 list->tail->next = item;
164 list->tail = item;
165 if (list->head == NULL)
166 list->head = item;
167 return item;
171 * Remove an element from the head of the list.
172 * Return this element.
175 void *
176 listdelhead (List *list)
178 Listitem *item = list->head;
179 void *ret;
181 if (item == NULL)
182 return NULL;
183 ret = item->data;
185 list->head = list->head->next;
186 if (list->head)
187 list->head->prev = NULL;
188 free(item);
189 if (list->tail == item)
190 list->tail = NULL;
191 return ret;
195 * Remove an element from the tail of the list.
196 * Return this element.
199 void *
200 listdeltail (List *list)
202 Listitem *item = list->tail;
203 void *ret;
205 if (item == NULL)
206 return NULL;
207 ret = item->data;
209 list->tail = list->tail->prev;
210 if (list->tail)
211 list->tail->next = NULL;
212 free (item);
213 if (list->head == item)
214 list->head = NULL;
215 return ret;
219 * listdel
222 void
223 listdel (List *list, Listitem *item)
225 if (item->prev)
226 item->prev->next = item->next;
227 if (item->next)
228 item->next->prev = item->prev;
229 if (item == list->head)
230 list->head = item->next;
231 if (item == list->tail)
232 list->tail = item->prev;
233 free (item);
237 * Iterate through all the items in a list.
240 void listiter (List *list, Bool (*fn)(List *, Listitem *, void *arg),
241 void *arg)
243 Listitem *item;
245 for (item = list->head; item; item = item->next)
246 if ((*fn) (list, item, arg))
247 break;