BLOG | DOCUMENTATION | TRAC

Home --> Documentations --> PJLIB Reference

Example: List Manipulation

Below is sample program to demonstrate how to manipulate linked list.

00001 /* $Id: list.c 3553 2011-05-05 06:14:19Z nanang $ */
00002 /* 
00003  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
00004  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00019  */
00020 #include <pj/list.h>
00021 #include <pj/assert.h>
00022 #include <pj/log.h>
00023 
00032 struct my_node
00033 {
00034     // This must be the first member declared in the struct!
00035     PJ_DECL_LIST_MEMBER(struct my_node);
00036     int value;
00037 };
00038 
00039 
00040 int main()
00041 {
00042     struct my_node nodes[10];
00043     struct my_node list;
00044     struct my_node *it;
00045     int i;
00046     
00047     // Initialize the list as empty.
00048     pj_list_init(&list);
00049     
00050     // Insert nodes.
00051     for (i=0; i<10; ++i) {
00052         nodes[i].value = i;
00053         pj_list_insert_before(&list, &nodes[i]);
00054     }
00055     
00056     // Iterate list nodes.
00057     it = list.next;
00058     while (it != &list) {
00059         PJ_LOG(3,("list", "value = %d", it->value));
00060         it = it->next;
00061     }
00062     
00063     // Erase all nodes.
00064     for (i=0; i<10; ++i) {
00065         pj_list_erase(&nodes[i]);
00066     }
00067     
00068     // List must be empty by now.
00069     pj_assert( pj_list_empty(&list) );
00070     
00071     return 0;
00072 };

 


PJLIB Open Source, high performance, small footprint, and very very portable framework
Copyright (C) 2006-2009 Teluu Inc.