摘要:linux内核源码真是好东东,是众多高手思维的结晶,在
linux 源代码中有个头文件为
list.h 。很多
linux 下的源代码都会使用这个头文件,它里面定义了一个结构
, 以及定义了和其相关的一组函数,这个结构是这样的:
[root@bdkyr cstudy]# cat double_list.c
#include
#include
#include "list.h"
struct int_node
{
/*视情况,增加*/
int val;
int num;
/************/
struct list_head list;
};
int main()
{
struct list_head head,*plist;
struct int_node a,b,c;
a.val = 1;
a.num = 1;
b.val = 2;
b.num = 2;
c.val = 3;
c.num = 3;
INIT_LIST_HEAD(&head); //初始化链表头
list_add_tail(&a.list,&head); //添加节点
list_add_tail(&b.list,&head);
list_add_tail(&c.list,&head);
printf("************遍历链表,打印结果**************\n");
list_for_each(plist,&head) //遍历链表,打印结果
{
struct int_node *node = list_entry(plist,struct int_node,list); //然后取得数据项,因此一般来说和list_for_each配合使用
printf("val = %d, num = %d\n", node->val, node->num);
}//print 1 1 2 2 3 3
printf("************删除节点b,重新遍历链表,打印结果*\n");
list_del(&b.list); //删除节点b
list_for_each(plist,&head) //重新遍历链表,打印结果
{
struct int_node *node = list_entry(plist,struct int_node,list);
printf("val = %d, num = %d\n", node->val, node->num);
}//print 1 1 3 3
printf("************打印链表head1******************\n");
struct int_node d,e;
struct list_head head1;
d.val = 4;
d.num = 4;
e.val = 5;
e.num = 5;
INIT_LIST_HEAD(&head1); //重新建立链表,表头为head1
list_add_tail(&d.list,&head1);
list_add_tail(&e.list,&head1);
list_for_each(plist,&head1)
{
struct int_node *node = list_entry(plist,struct int_node,list);
printf("val = %d, num = %d\n", node->val, node->num);
}
printf("*******************************************\n");
if(!list_empty(&head)) //判断链表是否为空
{
printf("the list is not empty!\n");
}
return 0;
}
struct list_head{
struct list_head *next, *prev;
};
如果您之前学过双向链表,那么当你看到这个结构的时候,会觉得似曾相识。岂止似曾相识,如果你看过Fio的源码,你会觉得它用的如此广泛,下面我们通过一个实例演示如何使用
一、编写代码[root@bdkyr cstudy]# cat double_list.c
#include
#include
#include "list.h"
struct int_node
{
/*视情况,增加*/
int val;
int num;
/************/
struct list_head list;
};
int main()
{
struct list_head head,*plist;
struct int_node a,b,c;
a.val = 1;
a.num = 1;
b.val = 2;
b.num = 2;
c.val = 3;
c.num = 3;
INIT_LIST_HEAD(&head); //初始化链表头
list_add_tail(&a.list,&head); //添加节点
list_add_tail(&b.list,&head);
list_add_tail(&c.list,&head);
printf("************遍历链表,打印结果**************\n");
list_for_each(plist,&head) //遍历链表,打印结果
{
struct int_node *node = list_entry(plist,struct int_node,list); //然后取得数据项,因此一般来说和list_for_each配合使用
printf("val = %d, num = %d\n", node->val, node->num);
}//print 1 1 2 2 3 3
printf("************删除节点b,重新遍历链表,打印结果*\n");
list_del(&b.list); //删除节点b
list_for_each(plist,&head) //重新遍历链表,打印结果
{
struct int_node *node = list_entry(plist,struct int_node,list);
printf("val = %d, num = %d\n", node->val, node->num);
}//print 1 1 3 3
printf("************打印链表head1******************\n");
struct int_node d,e;
struct list_head head1;
d.val = 4;
d.num = 4;
e.val = 5;
e.num = 5;
INIT_LIST_HEAD(&head1); //重新建立链表,表头为head1
list_add_tail(&d.list,&head1);
list_add_tail(&e.list,&head1);
list_for_each(plist,&head1)
{
struct int_node *node = list_entry(plist,struct int_node,list);
printf("val = %d, num = %d\n", node->val, node->num);
}
printf("*******************************************\n");
if(!list_empty(&head)) //判断链表是否为空
{
printf("the list is not empty!\n");
}
return 0;
}