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 listnode_add_before (struct list *list, struct listnode *pp, void *val)
167 assert (val != NULL);
169 nn = listnode_new ();
175 list->tail->next = nn;
179 nn->prev = list->tail;
200 /* Move given listnode to tail of the list */
202 listnode_move_to_tail (struct list *l, struct listnode *n)
204 LISTNODE_DETACH(l,n);
205 LISTNODE_ATTACH(l,n);
208 /* Delete specific date pointer from the list. */
210 listnode_delete (struct list *list, void *val)
212 struct listnode *node;
215 for (node = list->head; node; node = node->next)
217 if (node->data == val)
220 node->prev->next = node->next;
222 list->head = node->next;
225 node->next->prev = node->prev;
227 list->tail = node->prev;
230 listnode_free (node);
236 /* Return first node's data if it is there. */
238 listnode_head (struct list *list)
240 struct listnode *node;
250 /* Delete all listnode from the list. */
252 list_delete_all_node (struct list *list)
254 struct listnode *node;
255 struct listnode *next;
258 for (node = list->head; node; node = next)
262 (*list->del) (node->data);
263 listnode_free (node);
265 list->head = list->tail = NULL;
269 /* Delete all listnode then free list itself. */
271 list_delete (struct list *list)
274 list_delete_all_node (list);
278 /* Lookup the node which has given data. */
280 listnode_lookup (struct list *list, void *data)
282 struct listnode *node;
285 for (node = listhead(list); node; node = listnextnode (node))
286 if (data == listgetdata (node))
291 /* Delete the node from list. For ospfd and ospf6d. */
293 list_delete_node (struct list *list, struct listnode *node)
296 node->prev->next = node->next;
298 list->head = node->next;
300 node->next->prev = node->prev;
302 list->tail = node->prev;
304 listnode_free (node);
309 list_add_node_prev (struct list *list, struct listnode *current, void *val)
311 struct listnode *node;
313 assert (val != NULL);
315 node = listnode_new ();
316 node->next = current;
319 if (current->prev == NULL)
322 current->prev->next = node;
324 node->prev = current->prev;
325 current->prev = node;
332 list_add_node_next (struct list *list, struct listnode *current, void *val)
334 struct listnode *node;
336 assert (val != NULL);
338 node = listnode_new ();
339 node->prev = current;
342 if (current->next == NULL)
345 current->next->prev = node;
347 node->next = current->next;
348 current->next = node;
355 list_add_list (struct list *l, struct list *m)
359 for (n = listhead (m); n; n = listnextnode (n))
360 listnode_add (l, n->data);