Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 1 | /* $Id$ */
|
| 2 | /*
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 4 | *
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 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.
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 9 | *
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 10 | * This program is distributed in the hope that it will be useful,
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame] | 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
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 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 |
|
| 31 | struct 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 |
|
| 39 | int 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 | };
|