Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 操作系统 > Linux > glib库单向链表介绍
【标  题】:glib库单向链表介绍
【关键字】:glib
【来  源】:http://blog.csdn.net/plusboy/archive/2007/01/19/1487587.aspx

glib库单向链表介绍

Your Ad Here
glib库单向链表介绍

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

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

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

data成员定义为gpointer(即void*),可以放任何类型的数据。下面举个例子来说明怎么使用GSList来创建、添加、插入、排序、反转和销毁单向链表。
/*
 * file: g_slist_one.c
 * desc: 这个文件用于演示glib库里单向链表的创建、添加、插入、排序、反转与销毁
 * compile: gcc -o g_slist_one g_slist_one.c `pkg-config --cflags --libs glib-2.0`
 
*/


#include 
<glib.h>

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

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

    printf(
" ");
}


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


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

    
/*
     * 这里没有调用创建链表的函数,只需要声明一个指向GSList结构体的指针,
     * g_slist_append函数返回指向链表首部的指针,所以一定要保存这个指针
     
*/

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

    
/*
     * 在链表首部加入,复杂度是O(1),记得要保存函数返回的指针
     
*/

    printf(
"Add at head of list: ");
    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: ");
    list 
= g_slist_insert(list, "1"1);
    list 
= g_slist_insert(list, "2"2);
    list 
= g_slist_insert(list, "3"3);
    display_list(list);

    
/*
     * 向链表中插入节点并排序,这里我传入了一个用于排序链表元素的比较函数,
     * 这个函数只是简单的调用了strcmp。这里可以直接到strcmp作为第三个参数
     * 传给g_slist_insert_sorted
     
*/

    printf(
"Insert sorted: ");
    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: ");
    list 
= g_slist_reverse(list);
    display_list(list);

    
/*
     * 删除链表,如果list为NULL的话,g_slist_free函数会直接返回
     
*/

    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结构的指针就可以了,声明该指 针以后就可以用该指针来对链表进行操作,只需要记住每次对链表进行操作后,都要保存返回的链表头指针就可以了。

下面的一个例子用于演法对单链表的查找、删除与合并。
/*
 * file: g_slist_two.c
 * desc: 这个文件用于演示glib库单向链表的查找、删除与合并
 * compile: gcc -o g_slist_two g_slist_two.c `pkg-config --cflags --libs glib-2.0`
 
*/


#include 
<glib.h>

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

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

    printf(
" ");
}


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

    printf(
"Create single list: ");
    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;

    
/*
     * 查找内容为"three"的节点,返回指向找到的第一个节点的指针
     
*/

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

    
/*
     * 查找第三个节点,链表里节点的索引号为0,1,2...
     * 如果超出链表尾部(链表长度小于3)返回NULL
     
*/

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


    
/*
     * 取第5个节点的数据,如果超出链表尾部,返回NULL
     
*/

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

    list 
= g_slist_append(list, "two");
    display_list(list);
    
    
/*
     * 删除数据为"two"的节点,这里只会删除找到的第一个节点,
     * 后面的节点不会被删除
     
*/

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

    
/*
     * 另一种删除节点的方法,先把节点从链表里删除,再删除节点的数据
     
*/

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

    
/* 执行完下面这一步it->next==NULL */
    list 
= g_slist_remove_link(list, it);
    
    
/* 删除节点及其数据 */
    g_slist_free_1(it);
    display_list(list);

    GSList 
*list2 = NULL;
    
    printf(
"The second list: ");
    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: ");
    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
redhat 2.6.9-42.el to 2.6.9.19:【上一篇】
linux 下给firefox安装 java 插件:【下一篇】
【相关文章】
  • 使用CGLIB检测JavaBean被修改字段
  • uclibc和glibc的差别
  • glib库介绍
  • Taglib打包部署和使用方式
  • 发布一个taglib:+
  • 使用CGLIB轻松实现延迟加载(Lazyloader)
  • 使用CGLIB轻松实现延迟加载(Lazyload)
  • Glib 学习1
  • glibc
  • tomcat 标签 taglib
  • 【随机文章】
  • JScript 方法 - item 方法
  • 单网卡ADSL共享上网的设置
  • IDS欺骗之Fragroute(2)
  • 提高Linux系统安全性的十招<二>
  • 利用htmlparser抓取网页内容(一)
  • Vista新特征(Features)(英文)
  • Apache配置详解(最好的APACHE配置教程)
  • “斐波那契数列”问题的递推算法
  • 十天学会ASP之第九天
  • Windows XP系统组策略应用全接触
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.