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
;
83 /* Add new node with sort function. */
85 listnode_add_sort (struct list
*list
, void *val
)
90 new = listnode_new ();
95 for (n
= list
->head
; n
; n
= n
->next
)
97 if ((*list
->cmp
) (val
, n
->data
) < 0)
113 new->prev
= list
->tail
;
116 list
->tail
->next
= new;
125 listnode_add_after (struct list
*list
, struct listnode
*pp
, void *val
)
129 nn
= listnode_new ();
135 list
->head
->prev
= nn
;
139 nn
->next
= list
->head
;
159 /* Delete specific date pointer from the list. */
161 listnode_delete (struct list
*list
, void *val
)
163 struct listnode
*node
;
165 for (node
= list
->head
; node
; node
= node
->next
)
167 if (node
->data
== val
)
170 node
->prev
->next
= node
->next
;
172 list
->head
= node
->next
;
175 node
->next
->prev
= node
->prev
;
177 list
->tail
= node
->prev
;
180 listnode_free (node
);
186 /* Return first node's data if it is there. */
188 listnode_head (struct list
*list
)
190 struct listnode
*node
;
199 /* Delete all listnode from the list. */
201 list_delete_all_node (struct list
*list
)
203 struct listnode
*node
;
204 struct listnode
*next
;
206 for (node
= list
->head
; node
; node
= next
)
210 (*list
->del
) (node
->data
);
211 listnode_free (node
);
213 list
->head
= list
->tail
= NULL
;
217 /* Delete all listnode then free list itself. */
219 list_delete (struct list
*list
)
221 struct listnode
*node
;
222 struct listnode
*next
;
224 for (node
= list
->head
; node
; node
= next
)
228 (*list
->del
) (node
->data
);
229 listnode_free (node
);
234 /* Lookup the node which has given data. */
236 listnode_lookup (struct list
*list
, void *data
)
240 for (node
= list
->head
; node
; nextnode (node
))
241 if (data
== getdata (node
))
246 /* Delete the node from list. For ospfd and ospf6d. */
248 list_delete_node (list list
, 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 (list list
, listnode current
, void *val
)
266 struct listnode
*node
;
268 node
= listnode_new ();
269 node
->next
= current
;
272 if (current
->prev
== NULL
)
275 current
->prev
->next
= node
;
277 node
->prev
= current
->prev
;
278 current
->prev
= node
;
285 list_add_node_next (list list
, listnode current
, void *val
)
287 struct listnode
*node
;
289 node
= listnode_new ();
290 node
->prev
= current
;
293 if (current
->next
== NULL
)
296 current
->next
->prev
= node
;
298 node
->next
= current
->next
;
299 current
->next
= node
;
306 list_add_list (struct list
*l
, struct list
*m
)
310 for (n
= listhead (m
); n
; nextnode (n
))
311 listnode_add (l
, n
->data
);