blob: e478d220256acff345ef858015baf293cdd4a319 [file] [log] [blame]
Benny Prijonoe7224612005-11-13 19:40:44 +00001/* $Id$ */
2/*
3 * PJLIB - PJ Foundation Library
4 * (C)2003-2005 Benny Prijono <bennylp@bulukucing.org>
5 *
6 * Author:
7 * Benny Prijono <bennylp@bulukucing.org>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23#include <pj/list.h>
24#include <pj/assert.h>
25#include <pj/log.h>
26
27/**
28 * \page page_pjlib_samples_list_c Example: List Manipulation
29 *
30 * Below is sample program to demonstrate how to manipulate linked list.
31 *
32 * \includelineno pjlib-samples/list.c
33 */
34
35struct my_node
36{
37 // This must be the first member declared in the struct!
38 PJ_DECL_LIST_MEMBER(struct my_node);
39 int value;
40};
41
42
43int main()
44{
45 struct my_node nodes[10];
46 struct my_node list;
47 struct my_node *it;
48 int i;
49
50 // Initialize the list as empty.
51 pj_list_init(&list);
52
53 // Insert nodes.
54 for (i=0; i<10; ++i) {
55 nodes[i].value = i;
56 pj_list_insert_before(&list, &nodes[i]);
57 }
58
59 // Iterate list nodes.
60 it = list.next;
61 while (it != &list) {
62 PJ_LOG(3,("list", "value = %d", it->value));
63 it = it->next;
64 }
65
66 // Erase all nodes.
67 for (i=0; i<10; ++i) {
68 pj_list_erase(&nodes[i]);
69 }
70
71 // List must be empty by now.
72 pj_assert( pj_list_empty(&list) );
73
74 return 0;
75};