2 * gslist.c: Singly-linked list implementation
5 * Duncan Mak (duncan@novell.com)
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * (C) 2006 Novell, Inc.
35 return g_new0 (GSList, 1);
39 g_slist_free_1 (GSList *list)
45 g_slist_append (GSList *list, gpointer data)
47 return g_slist_concat (list, g_slist_prepend (NULL, data));
50 /* This is also a list node constructor. */
52 g_slist_prepend (GSList *list, gpointer data)
54 GSList *head = g_slist_alloc ();
62 g_slist_free (GSList *list)
67 g_slist_free_1 (list);
73 g_slist_copy (GSList *list)
80 copy = g_slist_prepend (NULL, list->data);
84 tmp->next = g_slist_prepend (NULL, list->next->data);
94 g_slist_concat (GSList *list1, GSList *list2)
97 g_slist_last (list1)->next = list2;
105 g_slist_foreach (GSList *list, GFunc func, gpointer user_data)
108 (*func) (list->data, user_data);
114 g_slist_last (GSList *list)
126 g_slist_find (GSList *list, gconstpointer data)
129 if (list->data == data)
139 g_slist_length (GSList *list)
152 g_slist_remove (GSList *list, gconstpointer data)
155 GSList *current = NULL;
160 if (list->data == data)
164 current = list->next;
167 if (current->data == data){
168 prev->next = current->next;
169 g_slist_free_1 (current);
174 current = current->next;
182 g_slist_remove_link (GSList *list, GSList *link)
185 GSList *current = NULL;
191 GSList *next = list->next;
197 current = list->next;
200 if (current == link){
201 prev->next = current->next;
202 current->next = NULL;
207 current = current->next;
214 g_slist_delete_link (GSList *list, GSList *link)
216 list = g_slist_remove_link (list, link);
217 g_slist_free_1 (link);
223 g_slist_reverse (GSList *list)
227 GSList *next = list->next;
237 g_slist_insert_sorted (GSList *list, gpointer data, GCompareFunc func)
239 GSList *current = NULL;
246 return g_slist_prepend (NULL, data);
248 if (func (list->data, data) > 0)
249 return g_slist_prepend (list, data);
252 current = list->next;
255 if (func (current->data, data) > 0){
256 prev->next = g_slist_prepend (current, data);
260 if (current->next == NULL){
261 g_slist_append (list, data);
266 current = current->next;