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. */
31 return XCALLOC (MTYPE_LINK_LIST
, sizeof (struct list
));
36 list_free (struct list
*l
)
38 XFREE (MTYPE_LINK_LIST
, l
);
41 /* Allocate new listnode. Internal use only. */
42 static struct listnode
*
45 return XCALLOC (MTYPE_LINK_NODE
, sizeof (struct listnode
));
50 listnode_free (struct listnode
*node
)
52 XFREE (MTYPE_LINK_NODE
, node
);
55 /* Add new data to the list. */
57 listnode_add (struct list
*list
, void *val
)
59 struct listnode
*node
;
63 node
= listnode_new ();
65 node
->prev
= list
->tail
;
68 if (list
->head
== NULL
)
71 list
->tail
->next
= node
;
78 * Add a node to the list. If the list was sorted according to the
79 * cmp function, insert a new node with the given val such that the
80 * list remains sorted. The new node is always inserted; there is no
81 * notion of omitting duplicates.
84 listnode_add_sort (struct list
*list
, void *val
)
91 new = listnode_new ();
96 for (n
= list
->head
; n
; n
= n
->next
)
98 if ((*list
->cmp
) (val
, n
->data
) < 0)
114 new->prev
= list
->tail
;
117 list
->tail
->next
= new;
126 listnode_add_after (struct list
*list
, struct listnode
*pp
, void *val
)
130 assert (val
!= NULL
);
132 nn
= listnode_new ();
138 list
->head
->prev
= nn
;
142 nn
->next
= list
->head
;
163 /* Delete specific date pointer from the list. */
165 listnode_delete (struct list
*list
, void *val
)
167 struct listnode
*node
;
170 for (node
= list
->head
; node
; node
= node
->next
)
172 if (node
->data
== val
)
175 node
->prev
->next
= node
->next
;
177 list
->head
= node
->next
;
180 node
->next
->prev
= node
->prev
;
182 list
->tail
= node
->prev
;
185 listnode_free (node
);
191 /* Return first node's data if it is there. */
193 listnode_head (struct list
*list
)
195 struct listnode
*node
;
205 /* Delete all listnode from the list. */
207 list_delete_all_node (struct list
*list
)
209 struct listnode
*node
;
210 struct listnode
*next
;
213 for (node
= list
->head
; node
; node
= next
)
217 (*list
->del
) (node
->data
);
218 listnode_free (node
);
220 list
->head
= list
->tail
= NULL
;
224 /* Delete all listnode then free list itself. */
226 list_delete (struct list
*list
)
229 list_delete_all_node (list
);
233 /* Lookup the node which has given data. */
235 listnode_lookup (struct list
*list
, void *data
)
237 struct listnode
*node
;
240 for (node
= listhead(list
); node
; node
= listnextnode (node
))
241 if (data
== listgetdata (node
))
246 /* Delete the node from list. For ospfd and ospf6d. */
248 list_delete_node (struct list
*list
, struct listnode
*node
)
251 node
->prev
->next
= node
->next
;
253 list
->head
= node
->next
;
255 node
->next
->prev
= node
->prev
;
257 list
->tail
= node
->prev
;
259 listnode_free (node
);
264 list_add_node_prev (struct list
*list
, struct listnode
*current
, void *val
)
266 struct listnode
*node
;
268 assert (val
!= NULL
);
270 node
= listnode_new ();
271 node
->next
= current
;
274 if (current
->prev
== NULL
)
277 current
->prev
->next
= node
;
279 node
->prev
= current
->prev
;
280 current
->prev
= node
;
287 list_add_node_next (struct list
*list
, struct listnode
*current
, void *val
)
289 struct listnode
*node
;
291 assert (val
!= NULL
);
293 node
= listnode_new ();
294 node
->prev
= current
;
297 if (current
->next
== NULL
)
300 current
->next
->prev
= node
;
302 node
->next
= current
->next
;
303 current
->next
= node
;
310 list_add_list (struct list
*l
, struct list
*m
)
314 for (n
= listhead (m
); n
; n
= listnextnode (n
))
315 listnode_add (l
, n
->data
);