1 /*****************************************************************************
2 * xlist.c : a simple doubly linked list in C
3 *****************************************************************************
4 * Copyright (C) 2003-2004 Commonwealth Scientific and Industrial Research
5 * Organisation (CSIRO) Australia
6 * Copyright (C) 2000-2004 the VideoLAN team
10 * Authors: Conrad Parker <Conrad.Parker@csiro.au>
11 * Andre Pang <Andre.Pang@csiro.au>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
33 xlist_node_new (void * data)
37 l = (XList *) malloc (sizeof (XList));
38 l->prev = l->next = NULL;
51 xlist_clone (XList * list)
53 XList * l, * new_list;
55 if (list == NULL) return NULL;
56 new_list = xlist_new ();
58 for (l = list; l; l = l->next) {
59 new_list = xlist_append (new_list, l->data);
66 xlist_clone_with (XList * list, XCloneFunc clone)
68 XList * l, * new_list;
71 if (list == NULL) return NULL;
72 if (clone == NULL) return xlist_clone (list);
74 new_list = xlist_new ();
76 for (l = list; l; l = l->next) {
77 new_data = clone (l->data);
78 new_list = xlist_append (new_list, new_data);
86 xlist_tail (XList * list)
89 for (l = list; l; l = l->next)
90 if (l->next == NULL) return l;
95 xlist_prepend (XList * list, void * data)
97 XList * l = xlist_node_new (data);
99 if (list == NULL) return l;
108 xlist_append (XList * list, void * data)
110 XList * l = xlist_node_new (data);
113 if (list == NULL) return l;
115 last = xlist_tail (list);
116 if (last) last->next = l;
122 xlist_add_before (XList * list, void * data, XList * node)
126 if (list == NULL) return xlist_node_new (data);
127 if (node == NULL) return xlist_append (list, data);
128 if (node == list) return xlist_prepend (list, data);
130 l = xlist_node_new (data);
142 xlist_add_after (XList * list, void * data, XList * node)
146 if (node == NULL) return xlist_prepend (list, data);
148 l = xlist_node_new (data);
160 xlist_find (XList * list, void * data)
164 for (l = list; l; l = l->next)
165 if (l->data == data) return l;
171 xlist_remove (XList * list, XList * node)
173 if (node == NULL) return list;
175 if (node->prev) node->prev->next = node->next;
176 if (node->next) node->next->prev = node->prev;
178 if (node == list) return list->next;
183 xlist_length (XList * list)
188 for (l = list; l; l = l->next)
195 xlist_is_empty (XList * list)
197 return (list == NULL);
201 xlist_is_singleton (XList * list)
203 if (list == NULL) return 0;
204 if (list->next == NULL) return 1;
209 * xlist_free_with (list, free_func)
211 * Step through list 'list', freeing each node using free_func(), and
212 * also free the list structure itself.
215 xlist_free_with (XList * list, XFreeFunc free_func)
219 for (l = list; l; l = ln) {
231 * Free the list structure 'list', but not its nodes.
234 xlist_free (XList * list)
238 for (l = list; l; l = ln) {