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内核-链表的更多相关文章

  1. C语言 Linux内核链表(企业级链表)

    //Linux内核链表(企业级链表) #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...

  2. 深入分析 Linux 内核链表--转

    引用地址:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据 ...

  3. Linux 内核链表

    一 . Linux内核链表 1 . 内核链表函数 1.INIT_LIST_HEAD:创建链表 2.list_add:在链表头插入节点 3.list_add_tail:在链表尾插入节点 4.list_d ...

  4. linux内核链表分析

    一.常用的链表和内核链表的区别 1.1  常规链表结构        通常链表数据结构至少应包含两个域:数据域和指针域,数据域用于存储数据,指针域用于建立与下一个节点的联系.按照指针域的组织以及各个节 ...

  5. 深入分析 Linux 内核链表

    转载:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/   一. 链表数据结构简介 链表是一种常用的组织有序数据的数据结构,它通过指 ...

  6. Linux 内核 链表 的简单模拟(2)

    接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the & ...

  7. Linux 内核 链表 的简单模拟(1)

    第零章:扯扯淡 出一个有意思的题目:用一个宏定义FIND求一个结构体struct里某个变量相对struc的编移量,如 struct student { int a; //FIND(struct stu ...

  8. linux内核链表的移植与使用

    一.  Linux内核链表为双向循环链表,和数据结构中所学链表类似,具体不再细讲.由于在内核中所实现的函数十分经典,所以移植出来方便后期应用程序中的使用. /********************* ...

  9. [国嵌攻略][108][Linux内核链表]

    链表简介 链表是一种常见的数据结构,它通过指针将一系列数据节点连接成一条数据链.相对于数组,链表具有更好的动态性,建立链表时无需预先知道数据总量,可以随机分配空间,可以高效地在链表中的任意位置实时插入 ...

  10. linux内核链表的使用

    linux内核链表:链表通常包括两个域:数据域和指针域.struct list_head{struct list_head *next,*prev;};include/linux/list.h中实现了 ...

随机推荐

  1. Android Studio新建Jni工程

    2.2版本的Android Studio支持新建Jni工程,不用再像以前自己构建工程目录,首先把自己的升级自己的AS到2.2以上 然后打开Tools->Andorid->SDK manag ...

  2. 项目解析- JspLibrary - part1

    http://rosspc:8080/JspLibrary/ 1. logon界面解析: JS 验证用户名.密码为空 <form name="form1" method=&q ...

  3. centos7 修改selinux 开机导致 faild to load SELinux policy freezing 错误

    centos7 修改selinux 开机导致 faild to load SELinux policy  freezing 错误 之前把selinux关闭了,这次想打开selinux,于是修改了 /e ...

  4. Mybatis学习(壹)

    一.Mybatis的引言 1.Mybatis框架概念:是数据库持久层的框架,对数据库的访问和操作.Mybatis对JDBC的封装,Mybatis替换JDBC开发,解决DAO中的通用问题. 2.JDBC ...

  5. MySQL 函数积累

    IFNULL(expr1,expr2) // 如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2.IFNULL()返回一个数字或字符串值 IF(expr1,expr2,e ...

  6. Loadrunner基础:Loadrunner Controller基本概念和使用

    Loadrnner Controller 介绍 当Vuser脚本开发完成以后,可以使用Controller将这个执行脚本的用户从单用户转化为多用户,从而模拟大量用户的操作,形成负载(多用户单循环,多用 ...

  7. border用处多

    1. 使用border属性实现梯形    给定一个div,通过设定div四个边框不同的颜色且设置比较粗的边框线条,可以看到div除了中间的content部分,四个边框均成梯形状,既然已经有了梯形的雏形 ...

  8. Problem 2020 组合(FOJ)

    Problem 2020 组合 Accept: 714    Submit: 1724Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem ...

  9. uva 1629

    1629 - Cake slicing Time limit: 3.000 seconds A rectangular cake with a grid of m * n <tex2html_v ...

  10. ios基础篇(五)——UITextField的详细使用

    UItextFieldField通常用于外部数据输入,以实现人机交互. 以下是UItextFieldField的属性及常见用法: 1.textField :设置文本框的默认文本. 2.Placehol ...