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