1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 linked lists containing integer values or pointers to data, with the ability
6 to iterate over the list in both directions.
8 <!-- ##### SECTION Long_Description ##### -->
10 The #GList structure and its associated functions provide a standard
11 doubly-linked list data structure.
14 Each element in the list contains a piece of data, together with pointers
15 which link to the previous and next elements in the list.
16 Using these pointers it is possible to move through the list in both
17 directions (unlike the
18 <link linkend="glib-Singly-Linked-lists">Singly-Linked Lists</link>
19 which only allows movement through the list in the forward direction).
22 The data contained in each element can be either integer values, by using one
24 <link linkend="glib-Type-Conversion-Macros">Type Conversion Macros</link>,
25 or simply pointers to any type of data.
28 List elements are allocated in blocks using a #GAllocator, which is
29 more efficient than allocating elements individually.
32 Note that most of the #GList functions expect to be passed a pointer to
33 the first element in the list. The functions which insert elements return
34 the new start of the list, which may have changed.
37 There is no function to create a #GList. %NULL is considered to be the empty
38 list so you simply set a #GList* to %NULL.
41 To add elements, use g_list_append(), g_list_prepend(), g_list_insert()
42 and g_list_insert_sorted().
45 To remove elements, use g_list_remove().
48 To find elements in the list use g_list_first(), g_list_last(), g_list_next(),
49 g_list_previous(), g_list_nth(), g_list_nth_data(), g_list_find() and
53 To find the index of an element use g_list_position() and g_list_index().
56 To call a function for each element in the list use g_list_foreach().
59 To free the entire list, use g_list_free().
62 <!-- ##### SECTION See_Also ##### -->
67 <!-- ##### STRUCT GList ##### -->
69 The #GList struct is used for each element in a doubly-linked list.
70 The <structfield>data</structfield> field holds the element's data, which can
71 be a pointer to any kind of data, or any integer value using the
72 <link linkend="glib-Type-Conversion-Macros">Type Conversion Macros</link>.
73 The <structfield>next</structfield> and <structfield>prev</structfield>
74 pointers are the links to the next and previous elements in the list.
81 <!-- ##### FUNCTION g_list_append ##### -->
83 Adds a new element on to the end of the list.
87 The return value is the new start of the list, which may have changed, so
88 make sure you store the new value.
91 <informalexample><programlisting>
92 /* Notice that these are initialized to the empty list. */
93 GList *list = NULL, *number_list = NULL;
95 /* This is a list of strings. */
96 list = g_list_append (list, "first");
97 list = g_list_append (list, "second");
99 /* This is a list of integers. */
100 number_list = g_list_append (number_list, GINT_TO_POINTER (27));
101 number_list = g_list_append (number_list, GINT_TO_POINTER (14));
102 </programlisting></informalexample>
104 @list: a pointer to a #GList.
105 @data: the data for the new element.
106 @Returns: the new start of the #GList.
109 <!-- ##### FUNCTION g_list_prepend ##### -->
111 Adds a new element on to the start of the list.
115 The return value is the new start of the list, which may have changed, so
116 make sure you store the new value.
119 <informalexample><programlisting>
120 /* Notice that it is initialized to the empty list. */
122 list = g_list_prepend (list, "last");
123 list = g_list_prepend (list, "first");
124 </programlisting></informalexample>
126 @list: a pointer to a #GList.
127 @data: the data for the new element.
128 @Returns: the new start of the #GList.
131 <!-- ##### FUNCTION g_list_insert ##### -->
133 Inserts a new element into the list at the given position.
136 @list: a pointer to a #GList.
137 @data: the data for the new element.
138 @position: the position to insert the element. If this is negative, or is
139 larger than the number of elements in the list, the new element is added on
140 to the end of the list.
141 @Returns: the new start of the #GList.
144 <!-- ##### FUNCTION g_list_insert_before ##### -->
146 Inserts a new element into the list before the given position.
149 @list: a pointer to a #GList.
150 @sibling: the list element before which the new element is inserted
151 or %NULL to insert at the end of the list.
152 @data: the data for the new element.
153 @Returns: the new start of the #GList.
156 <!-- ##### FUNCTION g_list_insert_sorted ##### -->
158 Inserts a new element into the list, using the given comparison function
159 to determine its position.
162 @list: a pointer to a #GList.
163 @data: the data for the new element.
164 @func: the function to compare elements in the list. It should return a
165 number > 0 if the first parameter comes after the second parameter in
167 @Returns: the new start of the #GList.
170 <!-- ##### FUNCTION g_list_remove ##### -->
172 Removes an element from a #GList.
173 If two elements contain the same data, only the first is removed.
174 If none of the elements contain the data, the #GList is unchanged.
178 @data: the data of the element to remove.
179 @Returns: the new start of the #GList.
182 <!-- ##### FUNCTION g_list_remove_link ##### -->
184 Removes an element from a #GList, without freeing the element.
185 The removed element's prev and next links are set to %NULL, so that it becomes a
186 self-contained list with one element.
190 @llink: an element in the #GList.
191 @Returns: the new start of the #GList, without the element.
194 <!-- ##### FUNCTION g_list_delete_link ##### -->
196 Deletes the node @link from @list.
200 @link: node to delete from @list.
201 @Returns: the new head of @list.
204 <!-- ##### FUNCTION g_list_remove_all ##### -->
206 Removes all list nodes with data equal to @data. Returns the new
207 head of the list. Contrast with g_list_remove() which removes only
208 the first node matching the given data.
212 @data: data to remove.
213 @Returns: new head of @list.
216 <!-- ##### FUNCTION g_list_free ##### -->
218 Frees all of the memory used by a #GList.
219 The freed elements are added to the #GAllocator free list.
223 If list elements contain dynamically-allocated memory, they should be freed
231 <!-- ##### FUNCTION g_list_alloc ##### -->
233 Allocates space for one #GList element.
234 It is called by g_list_append(), g_list_prepend(), g_list_insert() and
235 g_list_insert_sorted() and so is rarely used on its own.
238 @Returns: a pointer to the newly-allocated #GList element.
241 <!-- ##### FUNCTION g_list_free_1 ##### -->
243 Frees one #GList element.
244 It is usually used after g_list_remove_link().
247 @list: a #GList element.
250 <!-- ##### FUNCTION g_list_length ##### -->
252 Gets the number of elements in a #GList.
256 @Returns: the number of elements in the #GList.
259 <!-- ##### FUNCTION g_list_copy ##### -->
264 Note that this is a "shallow" copy. If the list elements consist of pointers
265 to data, the pointers are copied but the actual data isn't.
269 @Returns: a copy of @list.
272 <!-- ##### FUNCTION g_list_reverse ##### -->
275 It simply switches the next and prev pointers of each element.
279 @Returns: the start of the reversed #GList.
282 <!-- ##### FUNCTION g_list_sort ##### -->
284 Sorts a #GList using the given comparison function.
288 @compare_func: the comparison function used to sort the #GList. This function
289 is passed 2 elements of the #GList and should return 0 if they are equal,
290 a negative value if the first element comes before the second, or a positive
291 value if the first element comes after the second.
292 @Returns: the start of the sorted #GList.
295 <!-- ##### USER_FUNCTION GCompareFunc ##### -->
297 Specifies the type of a comparison function used to compare two
298 values. The function should return a negative integer if the first
299 value comes before the second, 0 if they are equal, or a positive
300 integer if the first value comes after the second.
304 @b: a value to compare with.
305 @Returns: negative value if @a < @b; zero if @a = @b; positive value
309 <!-- ##### FUNCTION g_list_sort_with_data ##### -->
311 Like g_list_sort(), but the comparison function accepts a user data argument.
315 @compare_func: comparison function.
316 @user_data: user data to pass to comparison function.
317 @Returns: the new head of @list.
320 <!-- ##### USER_FUNCTION GCompareDataFunc ##### -->
322 Specifies the type of a comparison function used to compare two
323 values. The function should return a negative integer if the first
324 value comes before the second, 0 if they are equal, or a positive
325 integer if the first value comes after the second.
329 @b: a value to compare with.
330 @user_data: user data to pass to comparison function.
331 @Returns: negative value if @a < @b; zero if @a = @b; positive value
335 <!-- ##### FUNCTION g_list_concat ##### -->
337 Adds the second #GList onto the end of the first #GList.
338 Note that the elements of the second #GList are not copied.
339 They are used directly.
343 @list2: the #GList to add to the end of the first #GList.
344 @Returns: the start of the new #GList.
347 <!-- ##### FUNCTION g_list_foreach ##### -->
349 Calls a function for each element of a #GList.
353 @func: the function to call with each element's data.
354 @user_data: user data to pass to the function.
357 <!-- ##### USER_FUNCTION GFunc ##### -->
359 Specifies the type of functions passed to g_list_foreach() and
363 @data: the element's data.
364 @user_data: user data passed to g_list_foreach() or g_slist_foreach().
367 <!-- ##### FUNCTION g_list_first ##### -->
369 Gets the first element in a #GList.
373 @Returns: the first element in a #GList, or %NULL if the #GList has no elements.
376 <!-- ##### FUNCTION g_list_last ##### -->
378 Gets the last element in a #GList.
382 @Returns: the last element in the #GList, or %NULL if the #GList has no
386 <!-- ##### MACRO g_list_previous ##### -->
388 A convenience macro to gets the previous element in a #GList.
391 @list: an element in a #GList.
392 @Returns: the previous element, or %NULL if there are no previous elements.
395 <!-- ##### MACRO g_list_next ##### -->
397 A convenience macro to gets the next element in a #GList.
400 @list: an element in a #GList.
401 @Returns: the next element, or %NULL if there are no more elements.
404 <!-- ##### FUNCTION g_list_nth ##### -->
406 Gets the element at the given position in a #GList.
410 @n: the position of the element, counting from 0.
411 @Returns: the element, or %NULL if the position is off the end of the #GList.
414 <!-- ##### FUNCTION g_list_nth_data ##### -->
416 Gets the data of the element at the given position.
420 @n: the position of the element.
421 @Returns: the element's data, or %NULL if the position is off the end of the
425 <!-- ##### FUNCTION g_list_nth_prev ##### -->
427 Gets the element @n places before @list.
431 @n: the position of the element, counting from 0.
432 @Returns: the element, or %NULL if the position is off the end of the #GList.
435 <!-- ##### FUNCTION g_list_find ##### -->
437 Finds the element in a #GList which contains the given data.
441 @data: the element data to find.
442 @Returns: the found #GList element, or %NULL if it is not found.
445 <!-- ##### FUNCTION g_list_find_custom ##### -->
447 Finds an element in a #GList, using a supplied function to find the desired
449 It iterates over the list, calling the given function which should return 0
450 when the desired element is found.
451 The function takes two #gconstpointer arguments, the #GList element's data
452 and the given user data.
456 @data: user data passed to the function.
457 @func: the function to call for each element. It should return 0 when the
458 desired element is found.
459 @Returns: the found #GList element, or %NULL if it is not found.
462 <!-- ##### FUNCTION g_list_position ##### -->
464 Gets the position of the given element in the #GList (starting from 0).
468 @llink: an element in the #GList.
469 @Returns: the position of the element in the #GList, or -1 if the element is
473 <!-- ##### FUNCTION g_list_index ##### -->
475 Gets the position of the element containing the given data (starting from 0).
479 @data: the data to find.
480 @Returns: the index of the element containing the data, or -1 if the data
484 <!-- ##### FUNCTION g_list_push_allocator ##### -->
486 Sets the allocator to use to allocate #GList elements.
487 Use g_list_pop_allocator() to restore the previous allocator.
490 @allocator: the #GAllocator to use when allocating #GList elements.
493 <!-- ##### FUNCTION g_list_pop_allocator ##### -->
495 Restores the previous #GAllocator, used when allocating #GList elements.