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. easyui combobox 智能提示搜索

    <!-- 获取机会点名称列表 --><script> function initOpportunityNameFuzzyQuery() { $('#jihuidianmingc ...

  2. 让你的linux操作系统更加安全【转】

    BIOS安全 记着要在BIOS设置中设定一个BIOS密码,不接收软盘启动.这样可以阻止不怀好意的人用专门的启动盘启动你的Linux系统,并避免别人更改BIOS设置,如更改软盘启动设置或不弹出密码框直接 ...

  3. 网页上记录鼠标的点击次数和一段有用的php代码,自己学习使用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 为什么MySQL死锁检测会严重降低TPS

    在大量的客户端,更新数据表的同一行时,会造成数据库的吞吐量大幅降低. 很多数据库的前辈和同行分别通过实验和源码的方法,定位到了罪魁祸首----MySQL死锁检测 实验方式:http://blog.cs ...

  5. golang为LigerUI编写简易版本web服务器

    package main import ( "io/ioutil" "log" "net/http" "os" ) va ...

  6. html5中的一些新语义标签

    <header> <nav> <ul> <li><a href="">栏目1</a></li> ...

  7. Django开发博客- 页面美化

    css是一种用来描述某种标记语言写的web站点的样式语言.这里我们并不想展开讨论,关于CSS我在这里推荐一个很不错的资源: Codeacademy HTML & CSS course 不想从头 ...

  8. 简单的Hibernate入门简介

    其实Hibernate本身是个独立的框架,它不需要任何web server或application server的支持.然而,大多数的Hibernate入门介绍都加入了很多非Hibernate的东西, ...

  9. 在ArcGIS 10.3标注中竖排文字

    ArcGIS 10.3中文字默认“从左至右”排列,而实际的标注比如“XX路.XX街”有些文字是竖直排列的. 接着就涉及到“标注样式”的问题. 如果是整体竖排,点击图层的Properties...(属性 ...

  10. DSP基础学习-ADC同步采样

    DSP基础学习-ADC同步采样 彭会锋 2015-04-28 20:31:06 在DSP28027 LauchPad学习过程中,关于ADC同步采样和顺序采样的区别稍加研究了一下,发现里面还真有些门道, ...