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 *****************************************************************************/
36 xlist_node_new (void * data)
40 l = (XList *) malloc (sizeof (XList));
41 l->prev = l->next = NULL;
54 xlist_clone (XList * list)
56 XList * l, * new_list;
58 if (list == NULL) return NULL;
59 new_list = xlist_new ();
61 for (l = list; l; l = l->next) {
62 new_list = xlist_append (new_list, l->data);
69 xlist_clone_with (XList * list, XCloneFunc clone)
71 XList * l, * new_list;
74 if (list == NULL) return NULL;
75 if (clone == NULL) return xlist_clone (list);
77 new_list = xlist_new ();
79 for (l = list; l; l = l->next) {
80 new_data = clone (l->data);
81 new_list = xlist_append (new_list, new_data);
89 xlist_tail (XList * list)
92 for (l = list; l; l = l->next)
93 if (l->next == NULL) return l;
98 xlist_prepend (XList * list, void * data)
100 XList * l = xlist_node_new (data);
102 if (list == NULL) return l;
111 xlist_append (XList * list, void * data)
113 XList * l = xlist_node_new (data);
116 if (list == NULL) return l;
118 last = xlist_tail (list);
125 xlist_add_before (XList * list, void * data, XList * node)
129 if (list == NULL) return xlist_node_new (data);
130 if (node == NULL) return xlist_append (list, data);
131 if (node == list) return xlist_prepend (list, data);
133 l = xlist_node_new (data);
145 xlist_add_after (XList * list, void * data, XList * node)
149 if (node == NULL) return xlist_prepend (list, data);
151 l = xlist_node_new (data);
163 xlist_find (XList * list, void * data)
167 for (l = list; l; l = l->next)
168 if (l->data == data) return l;
174 xlist_remove (XList * list, XList * node)
176 if (node == NULL) return list;
178 if (node->prev) node->prev->next = node->next;
179 if (node->next) node->next->prev = node->prev;
181 if (node == list) return list->next;
186 xlist_length (XList * list)
191 for (l = list; l; l = l->next)
198 xlist_is_empty (XList * list)
200 return (list == NULL);
204 xlist_is_singleton (XList * list)
206 if (list == NULL) return 0;
207 if (list->next == NULL) return 1;
212 * xlist_free_with (list, free_func)
214 * Step through list 'list', freeing each node using free_func(), and
215 * also free the list structure itself.
218 xlist_free_with (XList * list, XFreeFunc free_func)
222 for (l = list; l; l = ln) {
234 * Free the list structure 'list', but not its nodes.
237 xlist_free (XList * list)
241 for (l = list; l; l = ln) {