glib库单向链表

glib库单向链表介绍

glib库里实现了一些基本的数据结构,比如单向链表,双向链表、队列、树、hash表和数组。这篇文章里我主要介绍在linux平台下使用glib库中的单向链表进行编程,以后的文章我会陆续介绍双向链表、队列和其它数据结构的用法。

单向链表(即GSList)是glib库里最简单的容具,它把一系列的节点链接在一起,可以从一个节点访问到下一个节点。glib库里对GSList结构的定义如下:

struct  GSList
{
  gpointer data;
  GSList 
*next;
}
;

data成员定义为gpointer(即void*),可以放任何类型的数据。下面举个例子来说明怎么使用GSList来创建、添加、插入、排序、反转和销毁单向链表。



#include 
<</SPAN>glib.h>

void display_list(GSList *list)
{
    GSList 
*iterator = NULL;

    
for (iterator = list; iterator; iterator = iterator->next) {
        printf(
"%s "(char*)iterator->data);
    }
    printf(
"/n");
}

int my_str_cmp(gconstpointer str1, gconstpointer str2)
{
    
return strcmp(str1, str2);
}

int main(int argc, char *argv[])
{
    GSList 
*list = NULL;

    

    printf(
"Creat single list:/n");
    list 
= g_slist_append(list, "one");
    list 
= g_slist_append(list, "two");
    list 
= g_slist_append(list, "three");
    display_list(list);

    

    printf(
"Add at head of list:/n");
    list 
= g_slist_prepend(list, "first");
    list 
= g_slist_prepend(list, "second");
    list 
= g_slist_prepend(list, "third");
    display_list(list);

    

    printf(
"Insert at index 1, 2, 3:/n");
    list 
= g_slist_insert(list, "1"1);
    list 
= g_slist_insert(list, "2"2);
    list 
= g_slist_insert(list, "3"3);
    display_list(list);

    

    printf(
"Insert sorted:/n");
    list 
= g_slist_insert_sorted(list, "ONE"my_str_cmp);
    list 
= g_slist_insert_sorted(list, "TWO"my_str_cmp);
    list 
= g_slist_insert_sorted(list, "THREE"my_str_cmp);
    display_list(list);

    

    printf(
"Reverse the list:/n");
    list 
= g_slist_reverse(list);
    display_list(list);

    

    g_slist_free(list);
    
return 0;
}

程序的运行结果如下:

Creat single list:
one two three
Add at head of list:
third second first one two three
Insert at index 1, 2, 3:
third 1 2 3 second first one two three
Insert sorted:
ONE THREE TWO third 1 2 3 second first one two three
Reverse the list:
three two one first second 3 2 1 third TWO THREE ONE

从上面的例子里我们可以看到,使用一个单向链表前只需要声明一个指向GSList结构的指针就可以了,声明该指 针以后就可以用该指针来对链表进行操作,只需要记住每次对链表进行操作后,都要保存返回的链表头指针就可以了。

下面的一个例子用于演法对单链表的查找、删除与合并。


#include 
<</SPAN>glib.h>

void display_list(GSList *list)
{
    GSList 
*iterator = NULL;

    
for (iterator = list; iterator; iterator = iterator->next) {
        printf(
"%s "(char*iterator->data);
    }
    printf(
"/n");
}

int main(int argc, char *argv[])
{
    GSList 
*list = NULL;

    printf(
"Create single list:/n");
    list 
= g_slist_append(list, "one");
    list 
= g_slist_append(list, "two");
    list 
= g_slist_append(list, "three");
    list 
= g_slist_append(list, "four");
    list 
= g_slist_append(list, "five");
    display_list(list);

    GSList 
*it = NULL;

    

    printf(
"Find data "three" in the list:/n");
    it 
= g_slist_find(list, "three");
    printf(
"%s "(char*it->data);

    

    printf(
"Data of the 3rd item:/n");
    it 
= g_slist_nth(list, 2);
    
if (it != NULL) {
        printf(
"%s "(char*it->data);
    }

    

    printf(
"Data of the 5th item:/n");
    printf(
"%s "g_slist_nth_data(list, 4));

    list 
= g_slist_append(list, "two");
    display_list(list);
    
    

    printf(
"Remove the first data "two" from the list:/n");
    list 
= g_slist_remove(list, "two");
    display_list(list);

    

    printf(
"Remove the 3rd item from list:/n");
    it 
= g_slist_nth(list, 2);

    

    list 
= g_slist_remove_link(list, it);
    
    

    g_slist_free_1(it);
    display_list(list);

    GSList 
*list2 = NULL;
    
    printf(
"The second list:/n");
    list2 
= g_slist_append(list2, "six");
    list2 
= g_slist_append(list2, "seven");
    list2 
= g_slist_append(list2, "eight");
    display_list(list2);

    

    printf(
"Concat two lists:/n");
    list 
= g_slist_concat(list, list2);
    display_list(list);

    g_slist_free(list);
    
return 0;
}

程序的运行结果如下:

Create single list:
one two three four five
Find data "three" in the list:
three
Data of the 3rd item:
three
Data of the 5th item:
five
one two three four five two
Remove the first data "two" from the list:
one three four five two
Remove the 3rd item from list:
one three five two
The second list:
six seven eight
Concat two lists:
one three five two six seven eight

通过上面的两个子例我们可以看到,glib库中的单链表操作是很简单的。在这么多函数调用里,我们还可以发现glib库中操作数据结构的函数命名规则:g_容器名_函数名。对于我后面的文章将要讲到的双向链表、队列、树、hash表和数组,这一命名规则同样适用。

glib库的更多参考:
http://developer.gnome.org/doc/API/glib/index.html
http://developer.gnome.org/doc/API/2.0/glib/index.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值