blob: 127d8ddf82c4ea23e3aaa3b3e4069b39d8d02026 [file] [log] [blame]
Benny Prijono4766ffe2005-11-01 17:56:59 +00001/* $Id$
Benny Prijono4766ffe2005-11-01 17:56:59 +00002 */
Benny Prijonodd859a62005-11-01 16:42:51 +00003#include <pj/list.h>
4#include <pj/assert.h>
5#include <pj/log.h>
6
7/**
8 * \page page_pjlib_samples_list_c Example: List Manipulation
9 *
10 * Below is sample program to demonstrate how to manipulate linked list.
11 *
12 * \includelineno pjlib-samples/list.c
13 */
14
15struct my_node
16{
17 // This must be the first member declared in the struct!
Benny Prijonoa7f64a32005-11-07 15:47:28 +000018 PJ_DECL_LIST_MEMBER(struct my_node);
Benny Prijonodd859a62005-11-01 16:42:51 +000019 int value;
20};
21
22
23int main()
24{
25 struct my_node nodes[10];
26 struct my_node list;
27 struct my_node *it;
28 int i;
29
30 // Initialize the list as empty.
31 pj_list_init(&list);
32
33 // Insert nodes.
34 for (i=0; i<10; ++i) {
35 nodes[i].value = i;
36 pj_list_insert_before(&list, &nodes[i]);
37 }
38
39 // Iterate list nodes.
40 it = list.next;
41 while (it != &list) {
42 PJ_LOG(3,("list", "value = %d", it->value));
43 it = it->next;
44 }
45
46 // Erase all nodes.
47 for (i=0; i<10; ++i) {
48 pj_list_erase(&nodes[i]);
49 }
50
51 // List must be empty by now.
52 pj_assert( pj_list_empty(&list) );
53
54 return 0;
55};