This time the right fix.
[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 #GListAllocator, 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
156 </para>
157
158 @slist: 
159 @sibling: 
160 @data: 
161 @Returns: 
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
205 </para>
206
207 @list: 
208 @link: 
209 @Returns: 
210
211
212 <!-- ##### FUNCTION g_slist_free ##### -->
213 <para>
214 Frees all of the memory used by a #GSList.
215 The freed elements are added to the #GListAllocator free list.
216 </para>
217
218 @list: a #GSList.
219
220
221 <!-- ##### FUNCTION g_slist_free_1 ##### -->
222 <para>
223 Frees one #GSList element.
224 It is usually used after g_slist_remove_link().
225 </para>
226
227 @list: a #GSList element.
228
229
230 <!-- ##### FUNCTION g_slist_length ##### -->
231 <para>
232 Gets the number of elements in a #GSList.
233 </para>
234
235 @list: a #GSList.
236 @Returns: the number of elements in the #GSList.
237
238
239 <!-- ##### FUNCTION g_slist_copy ##### -->
240 <para>
241 Copies a #GSList.
242 </para>
243 <para>
244 Note that this is a "shallow" copy. If the list elements consist of pointers
245 to data, the pointers are copied but the actual data isn't.
246 </para>
247
248 @list: a #GSList.
249 @Returns: a copy of @list.
250
251
252 <!-- ##### FUNCTION g_slist_reverse ##### -->
253 <para>
254 Reverses a #GSList.
255 </para>
256
257 @list: a #GSList.
258 @Returns: the start of the reversed #GSList.
259
260
261 <!-- ##### FUNCTION g_slist_sort ##### -->
262 <para>
263 Sorts a #GSList using the given comparison function.
264 </para>
265
266 @list: a #GSList.
267 @compare_func: the comparison function used to sort the #GSList. This function
268 is passed 2 elements of the #GSList and should return 0 if they are equal,
269 a negative value if the first element comes before the second, or a positive
270 value if the first element comes after the second.
271 @Returns: the start of the sorted #GList.
272
273
274 <!-- ##### FUNCTION g_slist_concat ##### -->
275 <para>
276 Adds the second #GSList onto the end of the first #GSList.
277 Note that the elements of the second #GSList are not copied.
278 They are used directly.
279 </para>
280
281 @list1: a #GSList.
282 @list2: the #GSList to add to the end of the first #GSList.
283 @Returns: the start of the new #GSList.
284
285
286 <!-- ##### FUNCTION g_slist_foreach ##### -->
287 <para>
288 Calls a function for each element of a #GSList.
289 </para>
290
291 @list: a #GSList.
292 @func: the function to call with each element's data.
293 @user_data: user data to pass to the function.
294
295
296 <!-- ##### FUNCTION g_slist_last ##### -->
297 <para>
298 Gets the last element in a #GSList.
299 </para>
300
301 @list: a #GSList.
302 @Returns: the last element in the #GSList, or NULL if the #GSList has no
303 elements.
304
305
306 <!-- ##### MACRO g_slist_next ##### -->
307 <para>
308 A convenience macro to gets the next element in a #GSList.
309 </para>
310
311 @slist: an element in a #GSList.
312 @Returns: the next element, or NULL if there are no more elements.
313
314
315 <!-- ##### FUNCTION g_slist_nth ##### -->
316 <para>
317 Gets the element at the given position in a #GSList.
318 </para>
319
320 @list: a #GSList.
321 @n: the position of the element, counting from 0.
322 @Returns: the element, or NULL if the position is off the end of the #GSList.
323
324
325 <!-- ##### FUNCTION g_slist_nth_data ##### -->
326 <para>
327 Gets the data of the element at the given position.
328 </para>
329
330 @list: a #GSList.
331 @n: the position of the element.
332 @Returns: the element's data, or NULL if the position is off the end of the
333 #GSList.
334
335
336 <!-- ##### FUNCTION g_slist_find ##### -->
337 <para>
338 Finds the element in a #GSList which contains the given data.
339 </para>
340
341 @list: a #GSList.
342 @data: the element data to find.
343 @Returns: the found #GSList element, or NULL if it is not found.
344
345
346 <!-- ##### FUNCTION g_slist_find_custom ##### -->
347 <para>
348 Finds an element in a #GSList, using a supplied function to find the desired
349 element.
350 It iterates over the list, calling the given function which should return 0
351 when the desired element is found.
352 The function takes two #gconstpointer arguments, the #GSList element's data
353 and the given user data.
354 </para>
355
356 @list: a #GSList.
357 @data: user data passed to the function.
358 @func: the function to call for each element. It should return 0 when the
359 desired element is found.
360 @Returns: the found #GSList element, or NULL if it is not found.
361
362
363 <!-- ##### FUNCTION g_slist_position ##### -->
364 <para>
365 Gets the position of the given element in the #GSList (starting from 0).
366 </para>
367
368 @list: a #GSList.
369 @llink: an element in the #GSList.
370 @Returns: the position of the element in the #GSList, or -1 if the element
371 is not found.
372
373
374 <!-- ##### FUNCTION g_slist_index ##### -->
375 <para>
376 Gets the position of the element containing the given data (starting from 0).
377 </para>
378
379 @list: a #GSList.
380 @data: the data to find.
381 @Returns: the index of the element containing the data, or -1 if the data
382 is not found.
383
384
385 <!-- ##### FUNCTION g_slist_push_allocator ##### -->
386 <para>
387 Sets the allocator to use to allocate #GSList elements.
388 Use g_slist_pop_allocator() to restore the previous allocator.
389 </para>
390
391 @allocator: the #GAllocator to use when allocating #GSList elements.
392
393
394 <!-- ##### FUNCTION g_slist_pop_allocator ##### -->
395 <para>
396 Restores the previous #GAllocator, used when allocating #GSList elements.
397 </para>
398
399
400