8812d8a5202d3c85c663c63ee675202b4033d183
[platform/upstream/glib.git] / docs / reference / glib / tmpl / linked_lists_double.sgml
1 <!-- ##### SECTION Title ##### -->
2 Doubly-Linked Lists
3
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.
7
8 <!-- ##### SECTION Long_Description ##### -->
9 <para>
10 The #GList structure and its associated functions provide a standard
11 doubly-linked list data structure.
12 </para>
13 <para>
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).
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 #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.
35 </para>
36 <para>
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.
39 </para>
40 <para>
41 To add elements, use g_list_append(), g_list_prepend(), g_list_insert()
42 and g_list_insert_sorted().
43 </para>
44 <para>
45 To remove elements, use g_list_remove().
46 </para>
47 <para>
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
50 g_list_find_custom().
51 </para>
52 <para>
53 To find the index of an element use g_list_position() and g_list_index().
54 </para>
55 <para>
56 To call a function for each element in the list use g_list_foreach().
57 </para>
58 <para>
59 To free the entire list, use g_list_free().
60 </para>
61
62 <!-- ##### SECTION See_Also ##### -->
63 <para>
64
65 </para>
66
67 <!-- ##### STRUCT GList ##### -->
68 <para>
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.
75 </para>
76
77 @data: 
78 @next: 
79 @prev: 
80
81 <!-- ##### FUNCTION g_list_append ##### -->
82 <para>
83 Adds a new element on to the end of the list.
84 </para>
85 <note>
86 <para>
87 The return value is the new start of the list, which may have changed, so
88 make sure you store the new value.
89 </para>
90 </note>
91 <informalexample><programlisting>
92   /* Notice that these are initialized to the empty list. */
93   GList *list = NULL, *number_list = NULL;
94
95   /* This is a list of strings. */
96   list = g_list_append (list, "first");
97   list = g_list_append (list, "second");
98
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>
103
104 @list: a pointer to a #GList.
105 @data: the data for the new element.
106 @Returns: the new start of the #GList.
107
108
109 <!-- ##### FUNCTION g_list_prepend ##### -->
110 <para>
111 Adds a new element on to the start of the list.
112 </para>
113 <note>
114 <para>
115 The return value is the new start of the list, which may have changed, so
116 make sure you store the new value.
117 </para>
118 </note>
119 <informalexample><programlisting>
120   /* Notice that it is initialized to the empty list. */
121   GList *list = NULL;
122   list = g_list_prepend (list, "last");
123   list = g_list_prepend (list, "first");
124 </programlisting></informalexample>
125
126 @list: a pointer to a #GList.
127 @data: the data for the new element.
128 @Returns: the new start of the #GList.
129
130
131 <!-- ##### FUNCTION g_list_insert ##### -->
132 <para>
133 Inserts a new element into the list at the given position.
134 </para>
135
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.
142
143
144 <!-- ##### FUNCTION g_list_insert_sorted ##### -->
145 <para>
146 Inserts a new element into the list, using the given comparison function
147 to determine its position.
148 </para>
149
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
154 the sort order.
155 @Returns: the new start of the #GList.
156
157
158 <!-- ##### FUNCTION g_list_remove ##### -->
159 <para>
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.
163 </para>
164
165 @list: a #GList.
166 @data: the data of the element to remove.
167 @Returns: the new start of the #GList.
168
169
170 <!-- ##### FUNCTION g_list_remove_link ##### -->
171 <para>
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.
175 </para>
176
177 @list: a #GList.
178 @llink: an element in the #GList.
179 @Returns: the new start of the #GList, without the element.
180
181
182 <!-- ##### FUNCTION g_list_delete_link ##### -->
183 <para>
184
185 </para>
186
187 @list: 
188 @link: 
189 @Returns: 
190
191
192 <!-- ##### FUNCTION g_list_free ##### -->
193 <para>
194 Frees all of the memory used by a #GList.
195 The freed elements are added to the #GListAllocator free list.
196 </para>
197 <note>
198 <para>
199 If list elements contain dynamically-allocated memory, they should be freed
200 first.
201 </para>
202 </note>
203
204 @list: 
205
206
207 <!-- ##### FUNCTION g_list_alloc ##### -->
208 <para>
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.
212 </para>
213
214 @Returns: a pointer to the newly-allocated #GList element.
215
216
217 <!-- ##### FUNCTION g_list_free_1 ##### -->
218 <para>
219 Frees one #GList element.
220 It is usually used after g_list_remove_link().
221 </para>
222
223 @list: a #GList element.
224
225
226 <!-- ##### FUNCTION g_list_length ##### -->
227 <para>
228 Gets the number of elements in a #GList.
229 </para>
230
231 @list: a #GList.
232 @Returns: the number of elements in the #GList.
233
234
235 <!-- ##### FUNCTION g_list_copy ##### -->
236 <para>
237 Copies a #GList.
238 </para>
239 <para>
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.
242 </para>
243
244 @list: a #GList.
245 @Returns: a copy of @list.
246
247
248 <!-- ##### FUNCTION g_list_reverse ##### -->
249 <para>
250 Reverses a #GList.
251 It simply switches the next and prev pointers of each element.
252 </para>
253
254 @list: a #GList.
255 @Returns: the start of the reversed #GList.
256
257
258 <!-- ##### FUNCTION g_list_sort ##### -->
259 <para>
260 Sorts a #GList using the given comparison function.
261 </para>
262
263 @list: a #GList.
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.
269
270
271 <!-- ##### FUNCTION g_list_sort_with_data ##### -->
272 <para>
273
274 </para>
275
276 @list: 
277 @compare_func: 
278 @user_data: 
279 @Returns: 
280
281
282 <!-- ##### USER_FUNCTION GCompareFunc ##### -->
283 <para>
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.
288 </para>
289
290 @a: a value.
291 @b: a value to compare with.
292 @Returns: negative value if @a < @b; zero if @a = @b; positive value
293 if @a > @b.
294
295
296 <!-- ##### FUNCTION g_list_concat ##### -->
297 <para>
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.
301 </para>
302
303 @list1: a #GList.
304 @list2: the #GList to add to the end of the first #GList.
305 @Returns: the start of the new #GList.
306
307
308 <!-- ##### FUNCTION g_list_foreach ##### -->
309 <para>
310 Calls a function for each element of a #GList.
311 </para>
312
313 @list: a #GList.
314 @func: the function to call with each element's data.
315 @user_data: user data to pass to the function.
316
317
318 <!-- ##### USER_FUNCTION GFunc ##### -->
319 <para>
320 Specifies the type of functions passed to g_list_foreach() and
321 g_slist_foreach().
322 </para>
323
324 @data: the element's data.
325 @user_data: user data passed to g_list_foreach() or g_slist_foreach().
326
327
328 <!-- ##### FUNCTION g_list_first ##### -->
329 <para>
330 Gets the first element in a #GList.
331 </para>
332
333 @list: a #GList.
334 @Returns: the first element in a #GList, or NULL if the #GList has no elements.
335
336
337 <!-- ##### FUNCTION g_list_last ##### -->
338 <para>
339 Gets the last element in a #GList.
340 </para>
341
342 @list: a #GList.
343 @Returns: the last element in the #GList, or NULL if the #GList has no
344 elements.
345
346
347 <!-- ##### MACRO g_list_previous ##### -->
348 <para>
349 A convenience macro to gets the previous element in a #GList.
350 </para>
351
352 @list: an element in a #GList.
353 @Returns: the previous element, or NULL if there are no previous elements.
354
355
356 <!-- ##### MACRO g_list_next ##### -->
357 <para>
358 A convenience macro to gets the next element in a #GList.
359 </para>
360
361 @list: an element in a #GList.
362 @Returns: the next element, or NULL if there are no more elements.
363
364
365 <!-- ##### FUNCTION g_list_nth ##### -->
366 <para>
367 Gets the element at the given position in a #GList.
368 </para>
369
370 @list: 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.
373
374
375 <!-- ##### FUNCTION g_list_nth_data ##### -->
376 <para>
377 Gets the data of the element at the given position.
378 </para>
379
380 @list: a #GList.
381 @n: the position of the element.
382 @Returns: the element's data, or NULL if the position is off the end of the
383 #GList.
384
385
386 <!-- ##### FUNCTION g_list_find ##### -->
387 <para>
388 Finds the element in a #GList which contains the given data.
389 </para>
390
391 @list: a #GList.
392 @data: the element data to find.
393 @Returns: the found #GList element, or NULL if it is not found.
394
395
396 <!-- ##### FUNCTION g_list_find_custom ##### -->
397 <para>
398 Finds an element in a #GList, using a supplied function to find the desired
399 element.
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.
404 </para>
405
406 @list: a #GList.
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.
411
412
413 <!-- ##### FUNCTION g_list_position ##### -->
414 <para>
415 Gets the position of the given element in the #GList (starting from 0).
416 </para>
417
418 @list: a #GList.
419 @llink: an element in the #GList.
420 @Returns: the position of the element in the #GList, or -1 if the element is
421 not found.
422
423
424 <!-- ##### FUNCTION g_list_index ##### -->
425 <para>
426 Gets the position of the element containing the given data (starting from 0).
427 </para>
428
429 @list: a #GList.
430 @data: the data to find.
431 @Returns: the index of the element containing the data, or -1 if the data
432 is not found.
433
434
435 <!-- ##### FUNCTION g_list_push_allocator ##### -->
436 <para>
437 Sets the allocator to use to allocate #GList elements.
438 Use g_list_pop_allocator() to restore the previous allocator.
439 </para>
440
441 @allocator: the #GAllocator to use when allocating #GList elements.
442
443
444 <!-- ##### FUNCTION g_list_pop_allocator ##### -->
445 <para>
446 Restores the previous #GAllocator, used when allocating #GList elements.
447 </para>
448
449
450