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 #GListAllocator, 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_sorted ##### -->
146 Inserts a new element into the list, using the given comparison function
147 to determine its position.
150 @list: a pointer to a #GList.
151 @data: the data for the new element.
152 @func: the function to compare elements in the list. It should return a
153 number > 0 if the first parameter comes after the second parameter in
155 @Returns: the new start of the #GList.
158 <!-- ##### FUNCTION g_list_remove ##### -->
160 Removes an element from a #GList.
161 If two elements contain the same data, only the first is removed.
162 If none of the elements contain the data, the #GList is unchanged.
166 @data: the data of the element to remove.
167 @Returns: the new start of the #GList.
170 <!-- ##### FUNCTION g_list_remove_link ##### -->
172 Removes an element from a #GList, without freeing the element.
173 The removed element's prev and next links are set to NULL, so that it becomes a
174 self-contained list with one element.
178 @llink: an element in the #GList.
179 @Returns: the new start of the #GList, without the element.
182 <!-- ##### FUNCTION g_list_delete_link ##### -->
192 <!-- ##### FUNCTION g_list_free ##### -->
194 Frees all of the memory used by a #GList.
195 The freed elements are added to the #GListAllocator free list.
199 If list elements contain dynamically-allocated memory, they should be freed
207 <!-- ##### FUNCTION g_list_alloc ##### -->
209 Allocates space for one #GList element.
210 It is called by g_list_append(), g_list_prepend(), g_list_insert() and
211 g_list_insert_sorted() and so is rarely used on its own.
214 @Returns: a pointer to the newly-allocated #GList element.
217 <!-- ##### FUNCTION g_list_free_1 ##### -->
219 Frees one #GList element.
220 It is usually used after g_list_remove_link().
223 @list: a #GList element.
226 <!-- ##### FUNCTION g_list_length ##### -->
228 Gets the number of elements in a #GList.
232 @Returns: the number of elements in the #GList.
235 <!-- ##### FUNCTION g_list_copy ##### -->
240 Note that this is a "shallow" copy. If the list elements consist of pointers
241 to data, the pointers are copied but the actual data isn't.
245 @Returns: a copy of @list.
248 <!-- ##### FUNCTION g_list_reverse ##### -->
251 It simply switches the next and prev pointers of each element.
255 @Returns: the start of the reversed #GList.
258 <!-- ##### FUNCTION g_list_sort ##### -->
260 Sorts a #GList using the given comparison function.
264 @compare_func: the comparison function used to sort the #GList. This function
265 is passed 2 elements of the #GList and should return 0 if they are equal,
266 a negative value if the first element comes before the second, or a positive
267 value if the first element comes after the second.
268 @Returns: the start of the sorted #GList.
271 <!-- ##### FUNCTION g_list_sort_with_data ##### -->
282 <!-- ##### USER_FUNCTION GCompareFunc ##### -->
284 Specifies the type of a comparison function used to compare two
285 values. The function should return a negative integer if the first
286 value comes before the second, 0 if they are equal, or a positive
287 integer if the first value comes after the second.
291 @b: a value to compare with.
292 @Returns: negative value if @a < @b; zero if @a = @b; positive value
296 <!-- ##### FUNCTION g_list_concat ##### -->
298 Adds the second #GList onto the end of the first #GList.
299 Note that the elements of the second #GList are not copied.
300 They are used directly.
304 @list2: the #GList to add to the end of the first #GList.
305 @Returns: the start of the new #GList.
308 <!-- ##### FUNCTION g_list_foreach ##### -->
310 Calls a function for each element of a #GList.
314 @func: the function to call with each element's data.
315 @user_data: user data to pass to the function.
318 <!-- ##### USER_FUNCTION GFunc ##### -->
320 Specifies the type of functions passed to g_list_foreach() and
324 @data: the element's data.
325 @user_data: user data passed to g_list_foreach() or g_slist_foreach().
328 <!-- ##### FUNCTION g_list_first ##### -->
330 Gets the first element in a #GList.
334 @Returns: the first element in a #GList, or NULL if the #GList has no elements.
337 <!-- ##### FUNCTION g_list_last ##### -->
339 Gets the last element in a #GList.
343 @Returns: the last element in the #GList, or NULL if the #GList has no
347 <!-- ##### MACRO g_list_previous ##### -->
349 A convenience macro to gets the previous element in a #GList.
352 @list: an element in a #GList.
353 @Returns: the previous element, or NULL if there are no previous elements.
356 <!-- ##### MACRO g_list_next ##### -->
358 A convenience macro to gets the next element in a #GList.
361 @list: an element in a #GList.
362 @Returns: the next element, or NULL if there are no more elements.
365 <!-- ##### FUNCTION g_list_nth ##### -->
367 Gets the element at the given position in a #GList.
371 @n: the position of the element, counting from 0.
372 @Returns: the element, or NULL if the position is off the end of the #GList.
375 <!-- ##### FUNCTION g_list_nth_data ##### -->
377 Gets the data of the element at the given position.
381 @n: the position of the element.
382 @Returns: the element's data, or NULL if the position is off the end of the
386 <!-- ##### FUNCTION g_list_find ##### -->
388 Finds the element in a #GList which contains the given data.
392 @data: the element data to find.
393 @Returns: the found #GList element, or NULL if it is not found.
396 <!-- ##### FUNCTION g_list_find_custom ##### -->
398 Finds an element in a #GList, using a supplied function to find the desired
400 It iterates over the list, calling the given function which should return 0
401 when the desired element is found.
402 The function takes two #gconstpointer arguments, the #GList element's data
403 and the given user data.
407 @data: user data passed to the function.
408 @func: the function to call for each element. It should return 0 when the
409 desired element is found.
410 @Returns: the found #GList element, or NULL if it is not found.
413 <!-- ##### FUNCTION g_list_position ##### -->
415 Gets the position of the given element in the #GList (starting from 0).
419 @llink: an element in the #GList.
420 @Returns: the position of the element in the #GList, or -1 if the element is
424 <!-- ##### FUNCTION g_list_index ##### -->
426 Gets the position of the element containing the given data (starting from 0).
430 @data: the data to find.
431 @Returns: the index of the element containing the data, or -1 if the data
435 <!-- ##### FUNCTION g_list_push_allocator ##### -->
437 Sets the allocator to use to allocate #GList elements.
438 Use g_list_pop_allocator() to restore the previous allocator.
441 @allocator: the #GAllocator to use when allocating #GList elements.
444 <!-- ##### FUNCTION g_list_pop_allocator ##### -->
446 Restores the previous #GAllocator, used when allocating #GList elements.