2 * Copyright (c) 1995, 1996, 1997, 1998, 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
35 * List handling functions
49 * The representation is with a double-linked list, a pointer to
50 * the tail, and another one to the head.
60 List
*tmp
= (List
*)malloc (sizeof (List
));
63 tmp
->head
= tmp
->tail
= NULL
;
68 * Free a list, assume that its empty
74 list
->head
= list
->tail
= NULL
;
79 * Add an element before `item'
83 listaddbefore (List
*list
, Listitem
*old_item
, void *data
)
85 Listitem
*item
= (Listitem
*)malloc (sizeof (Listitem
));
91 item
->prev
= old_item
->prev
;
92 item
->next
= old_item
;
94 item
->prev
->next
= item
;
97 old_item
->prev
= item
;
102 * Add an element after `item'
106 listaddafter (List
*list
, Listitem
*old_item
, void *data
)
108 Listitem
*item
= (Listitem
*)malloc (sizeof (Listitem
));
114 item
->next
= old_item
->next
;
115 item
->prev
= old_item
;
117 item
->next
->prev
= item
;
120 old_item
->next
= item
;
125 * Add an element to the head of the list
129 listaddhead (List
*list
, void *data
)
131 Listitem
*item
= (Listitem
*)malloc (sizeof (Listitem
));
138 item
->next
= list
->head
;
140 list
->head
->prev
= item
;
142 if (list
->tail
== NULL
)
148 * Add an element to the tail of the list
152 listaddtail (List
*list
, void *data
)
154 Listitem
*item
= (Listitem
*)malloc (sizeof (Listitem
));
161 item
->prev
= list
->tail
;
163 list
->tail
->next
= item
;
165 if (list
->head
== NULL
)
171 * Remove an element from the head of the list.
172 * Return this element.
176 listdelhead (List
*list
)
178 Listitem
*item
= list
->head
;
185 list
->head
= list
->head
->next
;
187 list
->head
->prev
= NULL
;
189 if (list
->tail
== item
)
195 * Remove an element from the tail of the list.
196 * Return this element.
200 listdeltail (List
*list
)
202 Listitem
*item
= list
->tail
;
209 list
->tail
= list
->tail
->prev
;
211 list
->tail
->next
= NULL
;
213 if (list
->head
== item
)
223 listdel (List
*list
, Listitem
*item
)
226 item
->prev
->next
= 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
;
237 * Iterate through all the items in a list.
240 void listiter (List
*list
, Bool (*fn
)(List
*, Listitem
*, void *arg
),
245 for (item
= list
->head
; item
; item
= item
->next
)
246 if ((*fn
) (list
, item
, arg
))