1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 linked lists containing integer values or pointers to data, limited to
6 iterating over the list in one direction.
8 <!-- ##### SECTION Long_Description ##### -->
10 The #GSList structure and its associated functions provide a standard
11 singly-linked list data structure.
14 Each element in the list contains a piece of data, together with a pointer
15 which links to the next element in the list.
16 Using this pointer it is possible to move through the list in one
17 direction only (unlike the
18 <link linkend="glib-Doubly-Linked-lists">Doubly-Linked Lists</link>
19 which allow movement in both directions).
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 #GSList 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 #GSList. %NULL is considered to be the empty
38 list so you simply set a #GSList* to %NULL.
41 To add elements, use g_slist_append(), g_slist_prepend(), g_slist_insert()
42 and g_slist_insert_sorted().
45 To remove elements, use g_slist_remove().
48 To find elements in the list use g_slist_last(), g_slist_next(),
49 g_slist_nth(), g_slist_nth_data(), g_slist_find() and
50 g_slist_find_custom().
53 To find the index of an element use g_slist_position() and g_slist_index().
56 To call a function for each element in the list use g_slist_foreach().
59 To free the entire list, use g_slist_free().
62 <!-- ##### SECTION See_Also ##### -->
67 <!-- ##### STRUCT GSList ##### -->
69 The #GSList struct is used for each element in the singly-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> field contains the link to the next
80 <!-- ##### FUNCTION g_slist_alloc ##### -->
82 Allocates space for one #GSList element.
83 It is called by the g_slist_append(), g_slist_prepend(), g_slist_insert() and
84 g_slist_insert_sorted() functions and so is rarely used on its own.
87 @Returns: a pointer to the newly-allocated #GSList element.
90 <!-- ##### FUNCTION g_slist_append ##### -->
92 Adds a new element on to the end of the list.
96 The return value is the new start of the list, which may have changed, so
97 make sure you store the new value.
100 <informalexample><programlisting>
101 /* Notice that these are initialized to the empty list. */
102 GSList *list = NULL, *number_list = NULL;
104 /* This is a list of strings. */
105 list = g_slist_append (list, "first");
106 list = g_slist_append (list, "second");
108 /* This is a list of integers. */
109 number_list = g_slist_append (number_list, GINT_TO_POINTER (27));
110 number_list = g_slist_append (number_list, GINT_TO_POINTER (14));
111 </programlisting></informalexample>
114 @data: the data for the new element.
115 @Returns: the new start of the #GSList.
118 <!-- ##### FUNCTION g_slist_prepend ##### -->
120 Adds a new element on to the start of the list.
124 The return value is the new start of the list, which may have changed, so
125 make sure you store the new value.
128 <informalexample><programlisting>
129 /* Notice that it is initialized to the empty list. */
131 list = g_slist_prepend (list, "last");
132 list = g_slist_prepend (list, "first");
133 </programlisting></informalexample>
136 @data: the data for the new element.
137 @Returns: the new start of the #GSList.
140 <!-- ##### FUNCTION g_slist_insert ##### -->
142 Inserts a new element into the list at the given position.
146 @data: the data for the new element.
147 @position: the position to insert the element. If this is negative, or is
148 larger than the number of elements in the list, the new element is added on
149 to the end of the list.
150 @Returns: the new start of the #GSList.
153 <!-- ##### FUNCTION g_slist_insert_before ##### -->
155 Inserts a node before @sibling containing @data. Returns the new head of the list.
159 @sibling: node to insert @data before.
160 @data: data to put in the newly-inserted node.
161 @Returns: new head of the list.
164 <!-- ##### FUNCTION g_slist_insert_sorted ##### -->
166 Inserts a new element into the list, using the given comparison function
167 to determine its position.
171 @data: the data for the new element.
172 @func: the function to compare elements in the list. It should return a
173 number > 0 if the first parameter comes after the second parameter in
175 @Returns: the new start of the #GSList.
178 <!-- ##### FUNCTION g_slist_remove ##### -->
180 Removes an element from a #GSList.
181 If two elements contain the same data, only the first is removed.
182 If none of the elements contain the data, the #GSList is unchanged.
186 @data: the data of the element to remove.
187 @Returns: the new start of the #GSList.
190 <!-- ##### FUNCTION g_slist_remove_link ##### -->
192 Removes an element from a #GSList, without freeing the element.
193 The removed element's next link is set to %NULL, so that it becomes a
194 self-contained list with one element.
198 @link: an element in the #GSList.
199 @Returns: the new start of the #GSList, without the element.
202 <!-- ##### FUNCTION g_slist_delete_link ##### -->
204 Deletes a node of @list. Returns the new list head.
208 @link: node to delete.
209 @Returns: new head of @list.
212 <!-- ##### FUNCTION g_slist_remove_all ##### -->
214 Removes all list nodes with data equal to @data. Returns the new
215 head of the list. Contrast with g_slist_remove() which removes only
216 the first node matching the given data.
220 @data: data to remove.
221 @Returns: new head of @list.
224 <!-- ##### FUNCTION g_slist_free ##### -->
226 Frees all of the memory used by a #GSList.
227 The freed elements are added to the #GAllocator free list.
233 <!-- ##### FUNCTION g_slist_free_1 ##### -->
235 Frees one #GSList element.
236 It is usually used after g_slist_remove_link().
239 @list: a #GSList element.
242 <!-- ##### FUNCTION g_slist_length ##### -->
244 Gets the number of elements in a #GSList.
248 @Returns: the number of elements in the #GSList.
251 <!-- ##### FUNCTION g_slist_copy ##### -->
256 Note that this is a "shallow" copy. If the list elements consist of pointers
257 to data, the pointers are copied but the actual data isn't.
261 @Returns: a copy of @list.
264 <!-- ##### FUNCTION g_slist_reverse ##### -->
270 @Returns: the start of the reversed #GSList.
273 <!-- ##### FUNCTION g_slist_sort ##### -->
275 Sorts a #GSList using the given comparison function.
279 @compare_func: <function>qsort()</function>-style comparison function.
280 @Returns: the start of the sorted #GList.
283 <!-- ##### FUNCTION g_slist_sort_with_data ##### -->
285 Like g_slist_sort(), but the sort function accepts a user data argument.
289 @compare_func: comparison function.
290 @user_data: data to pass to comparison function.
291 @Returns: new head of the list.
294 <!-- ##### FUNCTION g_slist_concat ##### -->
296 Adds the second #GSList onto the end of the first #GSList.
297 Note that the elements of the second #GSList are not copied.
298 They are used directly.
302 @list2: the #GSList to add to the end of the first #GSList.
303 @Returns: the start of the new #GSList.
306 <!-- ##### FUNCTION g_slist_foreach ##### -->
308 Calls a function for each element of a #GSList.
312 @func: the function to call with each element's data.
313 @user_data: user data to pass to the function.
316 <!-- ##### FUNCTION g_slist_last ##### -->
318 Gets the last element in a #GSList.
322 @Returns: the last element in the #GSList, or %NULL if the #GSList has no
326 <!-- ##### MACRO g_slist_next ##### -->
328 A convenience macro to gets the next element in a #GSList.
331 @slist: an element in a #GSList.
332 @Returns: the next element, or %NULL if there are no more elements.
335 <!-- ##### FUNCTION g_slist_nth ##### -->
337 Gets the element at the given position in a #GSList.
341 @n: the position of the element, counting from 0.
342 @Returns: the element, or %NULL if the position is off the end of the #GSList.
345 <!-- ##### FUNCTION g_slist_nth_data ##### -->
347 Gets the data of the element at the given position.
351 @n: the position of the element.
352 @Returns: the element's data, or %NULL if the position is off the end of the
356 <!-- ##### FUNCTION g_slist_find ##### -->
358 Finds the element in a #GSList which contains the given data.
362 @data: the element data to find.
363 @Returns: the found #GSList element, or %NULL if it is not found.
366 <!-- ##### FUNCTION g_slist_find_custom ##### -->
368 Finds an element in a #GSList, using a supplied function to find the desired
370 It iterates over the list, calling the given function which should return 0
371 when the desired element is found.
372 The function takes two #gconstpointer arguments, the #GSList element's data
373 and the given user data.
377 @data: user data passed to the function.
378 @func: the function to call for each element. It should return 0 when the
379 desired element is found.
380 @Returns: the found #GSList element, or %NULL if it is not found.
383 <!-- ##### FUNCTION g_slist_position ##### -->
385 Gets the position of the given element in the #GSList (starting from 0).
389 @llink: an element in the #GSList.
390 @Returns: the position of the element in the #GSList, or -1 if the element
394 <!-- ##### FUNCTION g_slist_index ##### -->
396 Gets the position of the element containing the given data (starting from 0).
400 @data: the data to find.
401 @Returns: the index of the element containing the data, or -1 if the data
405 <!-- ##### FUNCTION g_slist_push_allocator ##### -->
407 Sets the allocator to use to allocate #GSList elements.
408 Use g_slist_pop_allocator() to restore the previous allocator.
411 @allocator: the #GAllocator to use when allocating #GSList elements.
414 <!-- ##### FUNCTION g_slist_pop_allocator ##### -->
416 Restores the previous #GAllocator, used when allocating #GSList elements.