1 /* Generic linked list routine.
2 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 /* Allocate new list. */
33 new = XMALLOC (MTYPE_LINK_LIST
, sizeof (struct list
));
34 memset (new, 0, sizeof (struct list
));
40 list_free (struct list
*l
)
42 XFREE (MTYPE_LINK_LIST
, l
);
45 /* Allocate new listnode. Internal use only. */
46 static struct listnode
*
49 struct listnode
*node
;
51 node
= XMALLOC (MTYPE_LINK_NODE
, sizeof (struct listnode
));
52 memset (node
, 0, sizeof (struct listnode
));
58 listnode_free (struct listnode
*node
)
60 XFREE (MTYPE_LINK_NODE
, node
);
63 /* Add new data to the list. */
65 listnode_add (struct list
*list
, void *val
)
67 struct listnode
*node
;
69 node
= listnode_new ();
71 node
->prev
= list
->tail
;
74 if (list
->head
== NULL
)
77 list
->tail
->next
= node
;
84 * Add a node to the list. If the list was sorted according to the
85 * cmp function, insert a new node with the given val such that the
86 * list remains sorted. The new node is always inserted; there is no
87 * notion of omitting duplicates.
90 listnode_add_sort (struct list
*list
, void *val
)
95 new = listnode_new ();
100 for (n
= list
->head
; n
; n
= n
->next
)
102 if ((*list
->cmp
) (val
, n
->data
) < 0)
118 new->prev
= list
->tail
;
121 list
->tail
->next
= new;
130 listnode_add_after (struct list
*list
, struct listnode
*pp
, void *val
)
134 nn
= listnode_new ();
140 list
->head
->prev
= nn
;
144 nn
->next
= list
->head
;
164 /* Delete specific date pointer from the list. */
166 listnode_delete (struct list
*list
, void *val
)
168 struct listnode
*node
;
171 for (node
= list
->head
; node
; node
= node
->next
)
173 if (node
->data
== val
)
176 node
->prev
->next
= node
->next
;
178 list
->head
= node
->next
;
181 node
->next
->prev
= node
->prev
;
183 list
->tail
= node
->prev
;
186 listnode_free (node
);
192 /* Return first node's data if it is there. */
194 listnode_head (struct list
*list
)
196 struct listnode
*node
;
206 /* Delete all listnode from the list. */
208 list_delete_all_node (struct list
*list
)
210 struct listnode
*node
;
211 struct listnode
*next
;
214 for (node
= list
->head
; node
; node
= next
)
218 (*list
->del
) (node
->data
);
219 listnode_free (node
);
221 list
->head
= list
->tail
= NULL
;
225 /* Delete all listnode then free list itself. */
227 list_delete (struct list
*list
)
230 list_delete_all_node (list
);
234 /* Lookup the node which has given data. */
236 listnode_lookup (struct list
*list
, void *data
)
238 struct listnode
*node
;
241 for (node
= listhead(list
); node
; node
= listnextnode (node
))
242 if (data
== listgetdata (node
))
247 /* Delete the node from list. For ospfd and ospf6d. */
249 list_delete_node (struct list
*list
, struct listnode
*node
)
252 node
->prev
->next
= node
->next
;
254 list
->head
= node
->next
;
256 node
->next
->prev
= node
->prev
;
258 list
->tail
= node
->prev
;
260 listnode_free (node
);
265 list_add_node_prev (struct list
*list
, struct listnode
*current
, void *val
)
267 struct listnode
*node
;
269 node
= listnode_new ();
270 node
->next
= current
;
273 if (current
->prev
== NULL
)
276 current
->prev
->next
= node
;
278 node
->prev
= current
->prev
;
279 current
->prev
= node
;
286 list_add_node_next (struct list
*list
, struct listnode
*current
, void *val
)
288 struct listnode
*node
;
290 node
= listnode_new ();
291 node
->prev
= current
;
294 if (current
->next
== NULL
)
297 current
->next
->prev
= node
;
299 node
->next
= current
->next
;
300 current
->next
= node
;
307 list_add_list (struct list
*l
, struct list
*m
)
311 for (n
= listhead (m
); n
; n
= listnextnode (n
))
312 listnode_add (l
, n
->data
);