Trivial s/foo/foo_/ fixes to make <glib.h> includable with -Wshadow
[platform/upstream/glib.git] / docs / reference / glib / tmpl / linked_lists_single.sgml
1 <!-- ##### SECTION Title ##### -->
2 Singly-Linked Lists
3
4 <!-- ##### SECTION Short_Description ##### -->
5 linked lists containing integer values or pointers to data, limited to
6 iterating over the list in one direction.
7
8 <!-- ##### SECTION Long_Description ##### -->
9 <para>
10 The #GSList structure and its associated functions provide a standard
11 singly-linked list data structure.
12 </para>
13 <para>
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).
20 </para>
21 <para>
22 The data contained in each element can be either integer values, by using one
23 of the
24 <link linkend="glib-Type-Conversion-Macros">Type Conversion Macros</link>,
25 or simply pointers to any type of data.
26 </para>
27 <para>
28 List elements are allocated in blocks using a #GAllocator, which is
29 more efficient than allocating elements individually.
30 </para>
31 <para>
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.
35 </para>
36 <para>
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.
39 </para>
40 <para>
41 To add elements, use g_slist_append(), g_slist_prepend(), g_slist_insert()
42 and g_slist_insert_sorted().
43 </para>
44 <para>
45 To remove elements, use g_slist_remove().
46 </para>
47 <para>
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().
51 </para>
52 <para>
53 To find the index of an element use g_slist_position() and g_slist_index().
54 </para>
55 <para>
56 To call a function for each element in the list use g_slist_foreach().
57 </para>
58 <para>
59 To free the entire list, use g_slist_free().
60 </para>
61
62 <!-- ##### SECTION See_Also ##### -->
63 <para>
64
65 </para>
66
67 <!-- ##### STRUCT GSList ##### -->
68 <para>
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
74 element in the list.
75 </para>
76
77 @data: 
78 @next: 
79
80 <!-- ##### FUNCTION g_slist_alloc ##### -->
81 <para>
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.
85 </para>
86
87 @Returns: a pointer to the newly-allocated #GSList element.
88
89
90 <!-- ##### FUNCTION g_slist_append ##### -->
91 <para>
92 Adds a new element on to the end of the list.
93 </para>
94 <note>
95 <para>
96 The return value is the new start of the list, which may have changed, so
97 make sure you store the new value.
98 </para>
99 </note>
100 <informalexample><programlisting>
101   /* Notice that these are initialized to the empty list. */
102   GSList *list = NULL, *number_list = NULL;
103
104   /* This is a list of strings. */
105   list = g_slist_append (list, "first");
106   list = g_slist_append (list, "second");
107
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>
112
113 @list: a #GSList.
114 @data: the data for the new element.
115 @Returns: the new start of the #GSList.
116
117
118 <!-- ##### FUNCTION g_slist_prepend ##### -->
119 <para>
120 Adds a new element on to the start of the list.
121 </para>
122 <note>
123 <para>
124 The return value is the new start of the list, which may have changed, so
125 make sure you store the new value.
126 </para>
127 </note>
128 <informalexample><programlisting>
129   /* Notice that it is initialized to the empty list. */
130   GSList *list = NULL;
131   list = g_slist_prepend (list, "last");
132   list = g_slist_prepend (list, "first");
133 </programlisting></informalexample>
134
135 @list: a #GSList.
136 @data: the data for the new element.
137 @Returns: the new start of the #GSList.
138
139
140 <!-- ##### FUNCTION g_slist_insert ##### -->
141 <para>
142 Inserts a new element into the list at the given position.
143 </para>
144
145 @list: a #GSList.
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.
151
152
153 <!-- ##### FUNCTION g_slist_insert_before ##### -->
154 <para>
155 Inserts a node before @sibling containing @data. Returns the new head of the list.
156 </para>
157
158 @slist: a #GSList.
159 @sibling: node to insert @data before.
160 @data: data to put in the newly-inserted node.
161 @Returns: new head of the list.
162
163
164 <!-- ##### FUNCTION g_slist_insert_sorted ##### -->
165 <para>
166 Inserts a new element into the list, using the given comparison function
167 to determine its position.
168 </para>
169
170 @list: a #GSList.
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
174 the sort order.
175 @Returns: the new start of the #GSList.
176
177
178 <!-- ##### FUNCTION g_slist_remove ##### -->
179 <para>
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.
183 </para>
184
185 @list: a #GSList.
186 @data: the data of the element to remove.
187 @Returns: the new start of the #GSList.
188
189
190 <!-- ##### FUNCTION g_slist_remove_link ##### -->
191 <para>
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.
195 </para>
196
197 @list: a #GSList.
198 @link_: an element in the #GSList.
199 @Returns: the new start of the #GSList, without the element.
200
201
202 <!-- ##### FUNCTION g_slist_delete_link ##### -->
203 <para>
204 Deletes a node of @list. Returns the new list head.
205 </para>
206
207 @list: a #GSList.
208 @link_: node to delete.
209 @Returns: new head of @list.
210
211
212 <!-- ##### FUNCTION g_slist_remove_all ##### -->
213 <para>
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.
217 </para>
218
219 @list: a #GSList.
220 @data: data to remove.
221 @Returns: new head of @list.
222
223
224 <!-- ##### FUNCTION g_slist_free ##### -->
225 <para>
226 Frees all of the memory used by a #GSList.
227 The freed elements are added to the #GAllocator free list.
228 </para>
229
230 @list: a #GSList.
231
232
233 <!-- ##### FUNCTION g_slist_free_1 ##### -->
234 <para>
235 Frees one #GSList element.
236 It is usually used after g_slist_remove_link().
237 </para>
238
239 @list: a #GSList element.
240
241
242 <!-- ##### FUNCTION g_slist_length ##### -->
243 <para>
244 Gets the number of elements in a #GSList.
245 </para>
246
247 @list: a #GSList.
248 @Returns: the number of elements in the #GSList.
249
250
251 <!-- ##### FUNCTION g_slist_copy ##### -->
252 <para>
253 Copies a #GSList.
254 </para>
255 <para>
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.
258 </para>
259
260 @list: a #GSList.
261 @Returns: a copy of @list.
262
263
264 <!-- ##### FUNCTION g_slist_reverse ##### -->
265 <para>
266 Reverses a #GSList.
267 </para>
268
269 @list: a #GSList.
270 @Returns: the start of the reversed #GSList.
271
272
273 <!-- ##### FUNCTION g_slist_sort ##### -->
274 <para>
275 Sorts a #GSList using the given comparison function.
276 </para>
277
278 @list: a #GSList.
279 @compare_func: <function>qsort()</function>-style comparison function.
280 @Returns: the start of the sorted #GList.
281
282
283 <!-- ##### FUNCTION g_slist_sort_with_data ##### -->
284 <para>
285 Like g_slist_sort(), but the sort function accepts a user data argument.
286 </para>
287
288 @list: a #GSList
289 @compare_func: comparison function.
290 @user_data: data to pass to comparison function.
291 @Returns: new head of the list.
292
293
294 <!-- ##### FUNCTION g_slist_concat ##### -->
295 <para>
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.
299 </para>
300
301 @list1: a #GSList.
302 @list2: the #GSList to add to the end of the first #GSList.
303 @Returns: the start of the new #GSList.
304
305
306 <!-- ##### FUNCTION g_slist_foreach ##### -->
307 <para>
308 Calls a function for each element of a #GSList.
309 </para>
310
311 @list: a #GSList.
312 @func: the function to call with each element's data.
313 @user_data: user data to pass to the function.
314
315
316 <!-- ##### FUNCTION g_slist_last ##### -->
317 <para>
318 Gets the last element in a #GSList.
319 </para>
320
321 @list: a #GSList.
322 @Returns: the last element in the #GSList, or %NULL if the #GSList has no
323 elements.
324
325
326 <!-- ##### MACRO g_slist_next ##### -->
327 <para>
328 A convenience macro to gets the next element in a #GSList.
329 </para>
330
331 @slist: an element in a #GSList.
332 @Returns: the next element, or %NULL if there are no more elements.
333
334
335 <!-- ##### FUNCTION g_slist_nth ##### -->
336 <para>
337 Gets the element at the given position in a #GSList.
338 </para>
339
340 @list: 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.
343
344
345 <!-- ##### FUNCTION g_slist_nth_data ##### -->
346 <para>
347 Gets the data of the element at the given position.
348 </para>
349
350 @list: a #GSList.
351 @n: the position of the element.
352 @Returns: the element's data, or %NULL if the position is off the end of the
353 #GSList.
354
355
356 <!-- ##### FUNCTION g_slist_find ##### -->
357 <para>
358 Finds the element in a #GSList which contains the given data.
359 </para>
360
361 @list: a #GSList.
362 @data: the element data to find.
363 @Returns: the found #GSList element, or %NULL if it is not found.
364
365
366 <!-- ##### FUNCTION g_slist_find_custom ##### -->
367 <para>
368 Finds an element in a #GSList, using a supplied function to find the desired
369 element.
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.
374 </para>
375
376 @list: a #GSList.
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.
381
382
383 <!-- ##### FUNCTION g_slist_position ##### -->
384 <para>
385 Gets the position of the given element in the #GSList (starting from 0).
386 </para>
387
388 @list: a #GSList.
389 @llink: an element in the #GSList.
390 @Returns: the position of the element in the #GSList, or -1 if the element
391 is not found.
392
393
394 <!-- ##### FUNCTION g_slist_index ##### -->
395 <para>
396 Gets the position of the element containing the given data (starting from 0).
397 </para>
398
399 @list: a #GSList.
400 @data: the data to find.
401 @Returns: the index of the element containing the data, or -1 if the data
402 is not found.
403
404
405 <!-- ##### FUNCTION g_slist_push_allocator ##### -->
406 <para>
407 Sets the allocator to use to allocate #GSList elements.
408 Use g_slist_pop_allocator() to restore the previous allocator.
409 </para>
410
411 @allocator: the #GAllocator to use when allocating #GSList elements.
412
413
414 <!-- ##### FUNCTION g_slist_pop_allocator ##### -->
415 <para>
416 Restores the previous #GAllocator, used when allocating #GSList elements.
417 </para>
418
419
420