blob: 5d33e58d51c76ec093e9db9ab11606a5037bdaf5 [file] [log] [blame]
Benny Prijono4766ffe2005-11-01 17:56:59 +00001/* $Id$
2 *
3 */
Benny Prijonodd859a62005-11-01 16:42:51 +00004/*
5 * $Log: /pjproject-0.3/pjlib/src/pjlib-samples/list.c $
6 *
7 * 2 10/14/05 12:26a Bennylp
8 * Finished error code framework, some fixes in ioqueue, etc. Pretty
9 * major.
10 *
11 * 1 10/10/05 5:12p Bennylp
12 * Created.
13 *
14 */
15
16#include <pj/list.h>
17#include <pj/assert.h>
18#include <pj/log.h>
19
20/**
21 * \page page_pjlib_samples_list_c Example: List Manipulation
22 *
23 * Below is sample program to demonstrate how to manipulate linked list.
24 *
25 * \includelineno pjlib-samples/list.c
26 */
27
28struct my_node
29{
30 // This must be the first member declared in the struct!
31 PJ_DECL_LIST_MEMBER(struct my_node)
32 int value;
33};
34
35
36int main()
37{
38 struct my_node nodes[10];
39 struct my_node list;
40 struct my_node *it;
41 int i;
42
43 // Initialize the list as empty.
44 pj_list_init(&list);
45
46 // Insert nodes.
47 for (i=0; i<10; ++i) {
48 nodes[i].value = i;
49 pj_list_insert_before(&list, &nodes[i]);
50 }
51
52 // Iterate list nodes.
53 it = list.next;
54 while (it != &list) {
55 PJ_LOG(3,("list", "value = %d", it->value));
56 it = it->next;
57 }
58
59 // Erase all nodes.
60 for (i=0; i<10; ++i) {
61 pj_list_erase(&nodes[i]);
62 }
63
64 // List must be empty by now.
65 pj_assert( pj_list_empty(&list) );
66
67 return 0;
68};