设为首页 加入收藏

TOP

C语言的那些小秘密之链表(四) (一)
2014-11-23 22:30:40 来源: 作者: 【 】 浏览:1
Tags:语言 些小 秘密

大多数的读者在学习编程语言的时候都不喜欢那些枯燥的文字描述,包括我自己在开始学习编程的时候也是这样,对于代码的热情远远高于文字,所以我在我写东西的时候也不喜欢用枯燥的文字描述来向读者讲解,更喜欢用代码加上适当的文字描述的方式进行讲解,因为有些东西可能用枯燥的文字描述半天还不如实实在在的给读者呈现出一段简单的代码,让读者理解得更加的透彻些。但是并不是说文字描述就没用,文字描述也很重要,只是绝大部分读者都更加的希望直接达到最终的效果,都想跳过那些中间的步骤。接下来我们接着上一篇博客《C语言的那些小秘密之链表(三)》的内容继续讲解linux内核双向循环链表。

特此说明:我会把我在文章中编写代码时候用到的头文件list.h上传到我的空间,免积分下载,有需要的读者可以自己去下载,当然也可以自己上网下载或者从自己安装的linux系统中得到。下载地址:http://download.csdn.net/user/bigloomy

static inline int list_empty(const struct list_head *head)

{

return head->next == head;

}

static inline int list_empty_careful(const struct list_head *head)

{

struct list_head *next = head->next;

return (next == head) && (next == head->prev);

}

list_empty()函数和list_empty_careful()函数都是用来检测链表是否为空的。但是稍有区别的就是第一个链表使用的检测方法是判断表头的结点的下一个结点是否为其本身,如果是则返回为true,否则返回false。第二个函数使用的检测方法是判断表头的前一个结点和后一个结点是否为其本身,如果同时满足则返回false,否则返回值为true。说多了可能读者就会没耐心了,那么接下来我来看看下面的代码。

#include

#include

#include "list.h"

typedef struct _stu

{

char name[20];

int num;

struct list_head list;

}stu;

int main()

{

stu *pstu;

stu *tmp_stu;

struct list_head stu_list;

struct list_head *pos;

int i = 0;

INIT_LIST_HEAD(&stu_list);

pstu = malloc(sizeof(stu)*5);

for(i=0;i<5;i++)

{

sprintf(pstu[i].name,"Stu%d",i+1);

pstu[i].num = i+1;

list_add( &(pstu[i].list), &stu_list);

}

list_for_each(pos,&stu_list)

{

tmp_stu = list_entry(pos, stu, list);

printf("student num: %d\tstudent name: %s\n",tmp_stu->num,tmp_stu->name);

}

if(list_empty(&stu_list))

printf("使用list_empty()检测,链表为空\n");

else

printf("使用list_empty()检测,链表非空\n");

if(list_empty_careful(&stu_list))

printf("使用list_empty_careful()检测,链表为空\n");

else

printf("使用list_empty_careful()检测,链表非空\n");

free(pstu);

return 0;

}

运行结果为:

root@ubuntu:/home/paixu/dlist_node# ./a

student num: 5 student name: Stu5

student num: 4 student name: Stu4

student num: 3 student name: Stu3

student num: 2 student name: Stu2

student num: 1 student name: Stu1

使用list_empty()检测,链表非空

使用list_empty_careful()检测,链表非空

看看代码就知道如何使用了,接下来看看链表的合成。

static inline void __list_splice(struct list_head *list,

struct list_head *head)

{

struct list_head *first = list->next;

struct list_head *last = list->prev;

struct list_head *at = head->next;

first->prev = head;

head->next = first;

last->next = at;

at->prev = last;

}

这个地方我觉得最好的方法就是使用图示来进行讲解,直观易懂,如果用文字描述半天还不如读者看一眼图。

将一个链表插入到另外一个链表中。不作链表是否为空的检查,由调用者默认保证。因为每个链表只有一个头节点,将空链表插入到另外一个链表中是没有意义的。但被插入的链表可以是空的。

static inline void list_splice(struct list_head *list, struct list_head *head)

{

if (!list_empty(list))

__list_splice(list, head);

}

在这种情况下会丢弃list所指向的头结点,因为两个链表有两个头结点,所以我们必须要去掉其中一个头结点。只要list非空链,head无任何限制,该函数就能实现链表的合并。

static inline void list_splice_init(struct list_head *list,

struct list_head *head)

{

if (!

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言的那些小秘密之链表(三) 下一篇让你不再害怕指针0--复杂类型说明

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: