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)的首指针.

最新文章

  1. js多种切换图片
  2. SAS零散知识总结
  3. DNS 正向查找与反向查找
  4. iOS中的两种主要架构及其优缺点浅析
  5. substr mb_substr mbstrct 的用法区别
  6. 重构14-Break Responsibilities
  7. Linux 输出重定向>和>>的区别是什么
  8. ASP.NET之自定义异步HTTP处理程序(图文教程)
  9. HDU 1039 -Easier Done Than Said?
  10. jquery插件分类与编写详细讲解
  11. C语言基础题
  12. CSS的标签类型
  13. 用windows性能监视器检测sqlserver 常见指标
  14. day14 Python集合关系运算交,差,并集
  15. mysql--SQL编程(关于mysql中的日期,实例,判断生日是否为闰年) 学习笔记2.1
  16. LUN挂载到Linux主机后,如何对磁盘进行分区
  17. linux 下 rpc python 实例之使用XML-RPC进行远程文件共享
  18. Apache Maven 入门篇
  19. 动态的把固定格式的json数据以菜单形式插入
  20. hdu 4547 LCA **

热门文章

  1. NDK与JNI
  2. SwitchCompat 修改颜色
  3. 任务栏右键工具栏里的语言栏没有的修复.reg
  4. xcode引入第三方静态类库 duplicate symbol _OBJC_XXX 重复编译错误
  5. java 集合(Collection 和 Array)
  6. kmeans算法原理以及实践操作(多种k值确定以及如何选取初始点方法)
  7. Excel中的隐藏函数
  8. Android 反编译工具简介
  9. #define && const
  10. python获取文件的内容