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
;
71 node
= listnode_new ();
73 node
->prev
= list
->tail
;
76 if (list
->head
== NULL
)
79 list
->tail
->next
= node
;
86 * Add a node to the list. If the list was sorted according to the
87 * cmp function, insert a new node with the given val such that the
88 * list remains sorted. The new node is always inserted; there is no
89 * notion of omitting duplicates.
92 listnode_add_sort (struct list
*list
, void *val
)
99 new = listnode_new ();
104 for (n
= list
->head
; n
; n
= n
->next
)
106 if ((*list
->cmp
) (val
, n
->data
) < 0)
122 new->prev
= list
->tail
;
125 list
->tail
->next
= new;
134 listnode_add_after (struct list
*list
, struct listnode
*pp
, void *val
)
138 assert (val
!= NULL
);
140 nn
= listnode_new ();
146 list
->head
->prev
= nn
;
150 nn
->next
= list
->head
;
171 /* Delete specific date pointer from the list. */
173 listnode_delete (struct list
*list
, void *val
)
175 struct listnode
*node
;
178 for (node
= list
->head
; node
; node
= node
->next
)
180 if (node
->data
== val
)
183 node
->prev
->next
= node
->next
;
185 list
->head
= node
->next
;
188 node
->next
->prev
= node
->prev
;
190 list
->tail
= node
->prev
;
193 listnode_free (node
);
199 /* Return first node's data if it is there. */
201 listnode_head (struct list
*list
)
203 struct listnode
*node
;
213 /* Delete all listnode from the list. */
215 list_delete_all_node (struct list
*list
)
217 struct listnode
*node
;
218 struct listnode
*next
;
221 for (node
= list
->head
; node
; node
= next
)
225 (*list
->del
) (node
->data
);
226 listnode_free (node
);
228 list
->head
= list
->tail
= NULL
;
232 /* Delete all listnode then free list itself. */
234 list_delete (struct list
*list
)
237 list_delete_all_node (list
);
241 /* Lookup the node which has given data. */
243 listnode_lookup (struct list
*list
, void *data
)
245 struct listnode
*node
;
248 for (node
= listhead(list
); node
; node
= listnextnode (node
))
249 if (data
== listgetdata (node
))
254 /* Delete the node from list. For ospfd and ospf6d. */
256 list_delete_node (struct list
*list
, struct listnode
*node
)
259 node
->prev
->next
= node
->next
;
261 list
->head
= node
->next
;
263 node
->next
->prev
= node
->prev
;
265 list
->tail
= node
->prev
;
267 listnode_free (node
);
272 list_add_node_prev (struct list
*list
, struct listnode
*current
, void *val
)
274 struct listnode
*node
;
276 assert (val
!= NULL
);
278 node
= listnode_new ();
279 node
->next
= current
;
282 if (current
->prev
== NULL
)
285 current
->prev
->next
= node
;
287 node
->prev
= current
->prev
;
288 current
->prev
= node
;
295 list_add_node_next (struct list
*list
, struct listnode
*current
, void *val
)
297 struct listnode
*node
;
299 assert (val
!= NULL
);
301 node
= listnode_new ();
302 node
->prev
= current
;
305 if (current
->next
== NULL
)
308 current
->next
->prev
= node
;
310 node
->next
= current
->next
;
311 current
->next
= node
;
318 list_add_list (struct list
*l
, struct list
*m
)
322 for (n
= listhead (m
); n
; n
= listnextnode (n
))
323 listnode_add (l
, n
->data
);