Chinaunix首页 | 论坛 | 博客
  • 博客访问: 473118
  • 博文数量: 58
  • 博客积分: 6047
  • 博客等级: 准将
  • 技术积分: 838
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-06 21:05
文章分类

全部博文(58)

文章存档

2009年(24)

2008年(34)

我的朋友

分类: LINUX

2008-12-22 16:14:03

glib学习笔记 2
使用glib提供的链表
转载请注明出处,或联系 fanyuanmail@126.com
在写程序中经常会用到一些对链表的操作,很多时候自己维护一套函数。其实在glib中有一个现成的list可以使用
Doubly-Linked Lists - linked lists containing integer values or pointers to data, with the ability to iterate over the list in both directions
Singly-Linked Lists - linked lists containing integer values or pointers to data, limited to iterating over the list in one direction

glib提供了一个双向链表和一个单向链表。下面这个例子是对链表进行追加,然后逆序,使用起来非常简单,现在越来越喜欢这个库了。

1.首先对链表增加了三个node
2.遍历整个链表
3.对链表逆序
4.遍历整个链表


1 #include
  2 int count=0;
  3
  4 void print_data(char* data)
  5 {
  6         count++;
  7         printf("count %d\n data is %s\n",count,data);
  8 }
  9
 10 int main(int argc, char *argv[])
 11 {
 12         GList* list=NULL;
 13         GList* newlist;
 14         list=g_list_append(list, "first");
 15         list=g_list_append(list, "second");
 16         list=g_list_append(list, "third");
//print_data要求传递一个函数
 17         g_list_foreach(list,print_data,list->data);
 18         g_printf("reverse the list\n");
 19         newlist=g_list_reverse(list);
 20         g_list_foreach(newlist,print_data,newlist->data);
 21         return 0;
 22 }

执行结果
[root@dhcp-cbjs05-218-247 glib_study]# ./glist_test
count 1
 data is first
count 2
 data is second
count 3
 data is third
reverse the list
count 4
 data is third
count 5
 data is second
count 6
 data is first
阅读(1837) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-06-05 23:55:34

专注于 glib 的 qq 技术讨论群 : 1959878 欢迎朋友们加入