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 *****************************************************************************/
32 xlist_node_new (void * data)
36 l = (XList *) malloc (sizeof (XList));
37 l->prev = l->next = NULL;
50 xlist_clone (XList * list)
52 XList * l, * new_list;
54 if (list == NULL) return NULL;
55 new_list = xlist_new ();
57 for (l = list; l; l = l->next) {
58 new_list = xlist_append (new_list, l->data);
65 xlist_clone_with (XList * list, XCloneFunc clone)
67 XList * l, * new_list;
70 if (list == NULL) return NULL;
71 if (clone == NULL) return xlist_clone (list);
73 new_list = xlist_new ();
75 for (l = list; l; l = l->next) {
76 new_data = clone (l->data);
77 new_list = xlist_append (new_list, new_data);
85 xlist_tail (XList * list)
88 for (l = list; l; l = l->next)
89 if (l->next == NULL) return l;
94 xlist_prepend (XList * list, void * data)
96 XList * l = xlist_node_new (data);
98 if (list == NULL) return l;
107 xlist_append (XList * list, void * data)
109 XList * l = xlist_node_new (data);
112 if (list == NULL) return l;
114 last = xlist_tail (list);
115 if (last) last->next = l;
121 xlist_add_before (XList * list, void * data, XList * node)
125 if (list == NULL) return xlist_node_new (data);
126 if (node == NULL) return xlist_append (list, data);
127 if (node == list) return xlist_prepend (list, data);
129 l = xlist_node_new (data);
141 xlist_add_after (XList * list, void * data, XList * node)
145 if (node == NULL) return xlist_prepend (list, data);
147 l = xlist_node_new (data);
159 xlist_find (XList * list, void * data)
163 for (l = list; l; l = l->next)
164 if (l->data == data) return l;
170 xlist_remove (XList * list, XList * node)
172 if (node == NULL) return list;
174 if (node->prev) node->prev->next = node->next;
175 if (node->next) node->next->prev = node->prev;
177 if (node == list) return list->next;
182 xlist_length (XList * list)
187 for (l = list; l; l = l->next)
194 xlist_is_empty (XList * list)
196 return (list == NULL);
200 xlist_is_singleton (XList * list)
202 if (list == NULL) return 0;
203 if (list->next == NULL) return 1;
208 * xlist_free_with (list, free_func)
210 * Step through list 'list', freeing each node using free_func(), and
211 * also free the list structure itself.
214 xlist_free_with (XList * list, XFreeFunc free_func)
218 for (l = list; l; l = ln) {
230 * Free the list structure 'list', but not its nodes.
233 xlist_free (XList * list)
237 for (l = list; l; l = ln) {