链表头必须在使用前用 INIT_LIST_HEAD 宏来初始化. 一个"要做的事情"的链表头可能声 明并且初始化用:

struct list_head todo_list; INIT_LIST_HEAD(&todo_list);

<para>可选地, 链表可在编译时初始化:</para>

LIST_HEAD(todo_list); 几个使用链表的函数定义在 <linux/list.h>:

list_add(struct list_head *new, struct list_head *head);

在紧接着链表 head 后面增加新入口项 -- 正常地在链表的开头. 因此, 它可用来 构建堆栈. 但是, 注意, head 不需要是链表名义上的头; 如果你传递一个 list_head 结构, 它在链表某处的中间, 新的项紧靠在它后面. 因为 Linux 链表 是环形的, 链表的头通常和任何其他的项没有区别.

list_add_tail(struct list_head *new, struct list_head *head);

刚好在给定链表头前面增加一个新入口项 -- 在链表的尾部, 换句话说. list_add_tail 能够, 因此, 用来构建先入先出队列.

list_del(struct list_head *entry); list_del_init(struct list_head *entry);

给定的项从队列中去除. 如果入口项可能注册在另外的链表中, 你应当使用 list_del_init, 它重新初始化这个链表指针.

list_move(struct list_head *entry, struct list_head *head); list_move_tail(struct list_head *entry, struct list_head *head);

给定的入口项从它当前的链表里去除并且增加到 head 的开始. 为安放入口项在新 链表的末尾, 使用
list_move_tail 代替.

list_empty(struct list_head *head); 如果给定链表是空, 返回一个非零值.

list_splice(struct list_head *list, struct list_head
*head); 将 list 紧接在 head 之后来连接 2 个链表.

list_head
结构对于实现一个相似结构的链表是好的, 但是调用程序常常感兴趣更大的结 构, 它组成链表作为一个整体. 一个宏定义, list_entry, 映射一个
list_head 结构指 针到一个指向包含它的结构的指针. 它如下被调用:

list_entry(struct
list_head *ptr, type_of_struct, field_name);

这里
ptr 是一个指向使用的 struct list_head 的指针, type_of_struct 是包含 ptr 的结构的类型, field_name 是结构中列表成员的名子.
在我们之前的 todo_struct 结构 中, 链表成员称为简单列表. 因此, 我们应当转变一个列表入口项为它的包含结构, 使用 这样一行:

struct todo_struct *todo_ptr = list_entry(listptr, struct
todo_struct, list); list_entry 宏定义使用了一些习惯的东西但是不难用.

链表的遍历是容易的: 只要跟随 prev 和 next 指针. 作为一个例子, 假设我们想保持
todo_struct 项的列表已降序的优先级顺序排列. 一个函数来添加新项应当看来如此:

void todo_add_entry(struct todo_struct *new)

{

struct list_head *ptr; struct todo_struct *entry;

for (ptr = todo_list.next; ptr != &todo_list; ptr
= ptr->next)

{

entry = list_entry(ptr, struct todo_struct, list); if
(entry->priority < new->priority) {

list_add_tail(&new->list, ptr); return;

}

}

list_add_tail(&new->list, &todo_struct)

}

但是, 作为一个通用的规则, 最好使用一个预定义的宏来创建循环, 它遍历链表. 前一个 循环, 例如, 可这样编码:

void todo_add_entry(struct todo_struct *new)

{

struct list_head *ptr; struct todo_struct *entry;

list_for_each(ptr, &todo_list)

{

entry = list_entry(ptr, struct todo_struct, list); if
(entry->priority < new->priority) {

list_add_tail(&new->list, ptr); return;

}

}

list_add_tail(&new->list, &todo_struct)

}

使用提供的宏帮助避免简单的编程错误; 宏的开发者也已做了些努力来保证它们进行正常. 存在几个变体:

list_for_each(struct
list_head *cursor, struct list_head *list)

这个宏创建一个 for 循环, 执行一次, cursor 指向链表中的每个连续的入口项. 小心改变列表在遍历它时.

list_for_each_prev(struct list_head *cursor, struct
list_head *list) 这个版本后向遍历链表.

list_for_each_safe(struct list_head *cursor, struct
list_head *next, struct list_head *list)

如果你的循环可能删除列表中的项, 使用这个版本. 它简单的存储列表
next 中下 一个项, 在循环的开始, 因此如果 cursor 指向的入口项被删除, 它不会被搞乱.

list_for_each_entry(type
*cursor, struct list_head *list, member) list_for_each_entry_safe(type *cursor,
type *next, struct list_head *list, member)

这些宏定义减轻了对一个包含给定结构类型的列表的处理. 这里, cursor 是一个 指向包含数据类型的指针, member 是包含结构中 list_head 结构的名子. 有了这 些宏, 没有必要安放 list_entry 调用在循环里了.

如果你查看 <linux/list.h> 里面, 你看到有一些附加的声明. hlist 类型是一个有一个 单独的, 单指针列表头类型的双向链表; 它常用作创建哈希表和类型结构. 还有宏用来遍 历 2 种列表类型, 打算作使用
读取-拷贝-更新 机制(在第 5 章的"读取-拷贝-更新"一 节中描述 ). 这些原语在设备驱动中不可能有用; 看头文件如果你愿意知道更多信息关于 它们是如何工作的.

Linux 内核链表头数据结构的更多相关文章

  1. 数据结构开发(10):Linux内核链表

    0.目录 1.老生常谈的两个宏(Linux) 1.1 offsetof 1.2 container_of 2.Linux内核链表剖析 3.小结 1.老生常谈的两个宏(Linux) Linux 内核中常 ...

  2. Linux内核链表深度分析【转】

    本文转载自:http://blog.csdn.net/coding__madman/article/details/51325646 链表简介: 链表是一种常用的数据结构,它通过指针将一系列数据节点连 ...

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

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

  4. linux内核链表分析

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

  5. 深入分析 Linux 内核链表

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

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

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

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

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

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

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

  9. linux内核链表的使用

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

随机推荐

  1. Linux的一些简单命令操作总结

    防火墙 查看防火墙状态 systemctl status iptables (或service iptables status) 关闭防火墙 systemctl stop iptables(或serv ...

  2. X-editable 不能二次初始化的问题解决方案

    最近用到了 X-editable 可编辑表格插件,发现了一个头疼的问题,X-editable 不能对同一个 <a> 元素二次初始化. 如下代码举例:在页面加载完成时,用“数组1”填充一个下 ...

  3. vagrant 安装 ubuntu

    安装版本: ubuntu  trusty64(14.04) step1: 安装vagrant,vbox step2: 下载box文件(官网http://www.vagrantbox.es/) http ...

  4. uml图的五种关系 标签: uml 2016-12-18 21:47 221人阅读 评论(25) 收藏

    统一建模语言 Unified Modeling Language (UML)又称统一建模语言或标准建模语言,是始于1997年一个OMG标准,它是一个支持模型化和软件系统开发的图形化语言,为软件开发的所 ...

  5. Spark in action on Kubernetes - 存储篇(一)

    前言 在上篇文章中,我们分析了Spark Operator内部的机制,今天我们会讨论一个在大数据领域中最重要的话题 - 存储.大数据已经无声无息的融入了每个人的生活中.大到旅游买房,小到外卖打车,都可 ...

  6. c++ 模板和traits

    #define TEST(ITEMNAME) AddItem(ITEMNAME, #ITEMNAME); template <typename T> void AddItem(T& ...

  7. MySQL按时间统计每个小时记录数

    MySQL按时间统计每个小时记录数 方案1: ? 1 2 3 4 5 6 7 SELECT  @rownum := @rownum + 1 AS ID,         CONCAT((CASE WH ...

  8. 关于mybatis中llike模糊查询中#和$的使用

    模糊查询: 工作中用到,写三种用法吧,第四种为大小写匹配查询 1. sql中字符串拼接 SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('% ...

  9. JavaWeb登录、注销、退出、记住用户名和密码

    应该是保存在Cookie里,session是放在服务器的内存里的.在用户关闭了网页窗口后,session就清空了.而Cookie是保存在用户的IE临时文件夹中的,再次登录时,读取其中的值传给服务器. ...

  10. 中国联通与阿里云达成合作,推动5G+新媒体产业发展

    4月24日在中国联通合作伙伴大会上,阿里云与中国联通签署合作协议,未来双方将基于各自优势,聚焦5G时代下的超高清视频发展. 随着5G时代到来,视频不再被网速制约,超短延时.计算节点下沉等特性将更高清. ...