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 <glib.h>
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
glib学习笔记2 使用glib链表
最新推荐文章于 2024-11-02 11:25:12 发布
本文介绍了如何使用GLib库中的双向链表功能,通过一个简单的示例展示了链表的创建、遍历及逆序操作。该示例采用C语言实现,并提供了完整的代码和运行结果。

1万+

被折叠的 条评论
为什么被折叠?



