Benny Prijono | 4766ffe | 2005-11-01 17:56:59 +0000 | [diff] [blame] | 1 | /* $Id$ |
Benny Prijono | 4766ffe | 2005-11-01 17:56:59 +0000 | [diff] [blame] | 2 | */ |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 3 | #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 | |
| 15 | struct my_node |
| 16 | { |
| 17 | // This must be the first member declared in the struct! |
Benny Prijono | a7f64a3 | 2005-11-07 15:47:28 +0000 | [diff] [blame] | 18 | PJ_DECL_LIST_MEMBER(struct my_node); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 19 | int value; |
| 20 | }; |
| 21 | |
| 22 | |
| 23 | int 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 | }; |