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