Linux内核-链表
linux内核链表的定义(定义了双向链表,不含数据域)
定义在 /linux-source-3.13.0/include/linux/types.h 头文件中.
struct list_head {
struct list_head *next, *prev;
};
我们可以利用这个数据结构定义含有数据域的链表,如:
struct my_list
{
void * mydata; //void * 可以指向任何类型的数据
struct list_head list; //隐藏了链表指针
}
链表的声明和初始化宏(list.h)
定义在 /linux-source-3.13.0/include/linux/list.h 中.
#define LIST_HEAD_INIT(name) { &(name), &(name) }
初始化一个名为name的双向链表的头节点.(name.pre = &name , name.next = &name)
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
通过调用LIST_HEAD,来声明和初始化一个自己的链表头,产生一个空链表.如:LIST_HEAD(mylist_head)
在链表添加一个节点
定义在 /linux-source-3.13.0/include/linux/list.h 中
具体实现:
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
#ifndef CONFIG_DEBUG_LIST
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
#else
extern void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next);
#endif /**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head) //在head节点后插入new节点,循环链表没有首尾,head可以是任意节点
{
__list_add(new, head, head->next);
} /**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head) //在head节点前插入new节点
{
__list_add(new, head->prev, head);
}
遍历链表
定义在 /linux-source-3.13.0/include/linux/list.h 中
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
它实际上是一个for循环,利用传入的pos作为循环变量,从表头开始逐顶向后(next方向)移动pos,直至回到head.
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
list_entry宏:从结构体(type)某成员变量(menber)指针(prt)来求出该结构体(type)的首指针.
Linux内核-链表的更多相关文章
- C语言 Linux内核链表(企业级链表)
//Linux内核链表(企业级链表) #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...
- 深入分析 Linux 内核链表--转
引用地址:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据 ...
- Linux 内核链表
一 . Linux内核链表 1 . 内核链表函数 1.INIT_LIST_HEAD:创建链表 2.list_add:在链表头插入节点 3.list_add_tail:在链表尾插入节点 4.list_d ...
- linux内核链表分析
一.常用的链表和内核链表的区别 1.1 常规链表结构 通常链表数据结构至少应包含两个域:数据域和指针域,数据域用于存储数据,指针域用于建立与下一个节点的联系.按照指针域的组织以及各个节 ...
- 深入分析 Linux 内核链表
转载:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/ 一. 链表数据结构简介 链表是一种常用的组织有序数据的数据结构,它通过指 ...
- Linux 内核 链表 的简单模拟(2)
接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the & ...
- Linux 内核 链表 的简单模拟(1)
第零章:扯扯淡 出一个有意思的题目:用一个宏定义FIND求一个结构体struct里某个变量相对struc的编移量,如 struct student { int a; //FIND(struct stu ...
- linux内核链表的移植与使用
一. Linux内核链表为双向循环链表,和数据结构中所学链表类似,具体不再细讲.由于在内核中所实现的函数十分经典,所以移植出来方便后期应用程序中的使用. /********************* ...
- [国嵌攻略][108][Linux内核链表]
链表简介 链表是一种常见的数据结构,它通过指针将一系列数据节点连接成一条数据链.相对于数组,链表具有更好的动态性,建立链表时无需预先知道数据总量,可以随机分配空间,可以高效地在链表中的任意位置实时插入 ...
- linux内核链表的使用
linux内核链表:链表通常包括两个域:数据域和指针域.struct list_head{struct list_head *next,*prev;};include/linux/list.h中实现了 ...
随机推荐
- er6855的工作经验
1 VIEWS里面的关系要搞清楚 里面的内容类型要理清 不要相信别人做好的事情 不要相信看到的结果 2 git rm -rf之后需要git commit提交到.git文件中正式生效 不然可能就是中间打 ...
- golang作为server向android提供数据服务
中间交换的数据是json ,后台数据库服务器是sqlserver2012 android通过post或者get方式访问 如get方式http://192.168.255.13:7080/tblFile ...
- JUnit org.junit.runner.Request.classWithoutSuiteMethod解决方法
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- 利用JAXB实现java实体类和xml互相转换
1.应用场景 在使用WebService实现数据上传下载,数据查询时,可以利用JAXB实现java实体类和xml互相转换 2.Demo 2.1 student.java 实体类,包含list(set同 ...
- 【BZOJ 2818】Gcd - 筛法求素数&phi()
题目描述 给定整数,求且为素数的数对有多少对. 分析 首先筛出所有的素数. 我们考虑枚举素数p,统计满足的个数,等价于统计的个数,即统计以内满足互质的有序数对个数. 不难发现,也就是说,我们只要预处理 ...
- CentOS怎样查看系统信息
一.查看系统版本和核心版本 1 登陆CentOS,启动终端. 2 登陆root帐户,输入 cat /etc/redhat-release,即可显示系统版本. 3 输入 uname -r ,可以查询 ...
- ArcMap中"开始编辑"遇到一个或多个带有警告的图层“如果继续,可能无法编辑某些图层”的警告框
开始编辑后可能出现的错误: 如果 ArcMap 在所选数据上启动编辑会话时遇到问题,将弹出一个对话框以提供附加信息.您可能会收到错误.警告或信息消息. 出现错误 时用户不可以启动任何编辑会话.只有解 ...
- 增强Web可用性,你需要避免的七大设计错误
Web设计给了你展示自我创新才能的平台,同时也要求你特别关注其中的诸多琐碎细节.优秀的Web设计师需要从设计前辈那里获得设计灵感,寻求他们给的建议,并反复推敲,以及付出诸多努力.职业博客作者Rajni ...
- js-- 一些题目
1. ~~3.14~~3.14=-((~3.14)+1)=-(-(3.14+1)+1)=-(-(3+1)+1)=-(-4+1) =-(-3)=3 按位非(NOT)(~)操作数的负值减1. 2. var ...
- 218. The Skyline Problem *HARD* -- 矩形重叠
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...