Linux最新Linux内核源码中最常见的数据结构之【list_head】,太赞了

最全的Linux教程,Linux从入门到精通

======================

  1. linux从入门到精通(第2版)

  2. Linux系统移植

  3. Linux驱动开发入门与实战

  4. LINUX 系统移植 第2版

  5. Linux开源网络全栈详解 从DPDK到OpenFlow

华为18级工程师呕心沥血撰写3000页Linux学习笔记教程

第一份《Linux从入门到精通》466页

====================

内容简介

====

本书是获得了很多读者好评的Linux经典畅销书**《Linux从入门到精通》的第2版**。本书第1版出版后曾经多次印刷,并被51CTO读书频道评为“最受读者喜爱的原创IT技术图书奖”。本书第﹖版以最新的Ubuntu 12.04为版本,循序渐进地向读者介绍了Linux 的基础应用、系统管理、网络应用、娱乐和办公、程序开发、服务器配置、系统安全等。本书附带1张光盘,内容为本书配套多媒体教学视频。另外,本书还为读者提供了大量的Linux学习资料和Ubuntu安装镜像文件,供读者免费下载。

华为18级工程师呕心沥血撰写3000页Linux学习笔记教程

本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。

需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

const typeof( ((type *)0)->member ) *__mptr 定义了一个member数据类型的指针

(char *)__mptr获取__mptr在内存中的地址

(char *)__mptr - offsetof(type,member)获取该结构体首地址

(type *)( (char *)__mptr - offsetof(type,member) )显式类型转化为type类型,任务完成!

现在通过container_of(S->others.next, struct student, others)就可以获取前一位同学的结构体啦

2. 相关方法

对于双向链表,除了上述基本操作,还包括初始化链表、添加节点、删除节点、更新节点、遍历链表等操作。这些方法也都被定义在内核中。

  1. 初始化链表
//创建头节点
#define LIST\_HEAD\_INIT(name) { &(name), &(name) }

#define LIST\_HEAD(name) \
 struct list\_head name = LIST\_HEAD\_INIT(name)

static inline void INIT\_LIST\_HEAD(struct list\_head \*list)
{
	list->next = list;
	list->prev = list;
}

  1. 添加节点
/\*
\* 在两个已知的连续节点之间添加新节点
\*/
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;
}

/\*\*
 \* list\_add - add a new entry
 \* @new: new entry to be added
 \* @head: list head to add it after
 \* 在指定的头节点之后插入新的节点
 \*/
static inline void list\_add(struct list\_head \*new, struct list\_head \*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
 \* 在特定的头节点之前插入新的节点
 \* 对用于实现队列非常有用
 \*/
static inline void list\_add\_tail(struct list\_head \*new, struct list\_head \*head)
{
	\_\_list\_add(new, head->prev, head);
}

  1. 删除节点
/\*
\* 删除两个已知节点之间的节点
\*/
static inline void \_\_list\_del(struct list\_head \* prev, struct list\_head \* next)
{
	next->prev = prev;
	prev->next = next;
}

static inline void \_\_list\_del\_entry(struct list\_head \*entry)
{
	\_\_list\_del(entry->prev, entry->next);
}

static inline void list\_del(struct list\_head \*entry)
{
	\_\_list\_del(entry->prev, entry->next);
	entry->next = LIST_POISON1;
	entry->prev = LIST_POISON2;
}

  1. 更新节点
/\*\*
 \* list\_replace - replace old entry by new one
 \* @old : the element to be replaced
 \* @new : the new element to insert
 \*
 \* If @old was empty, it will be overwritten.
 \*/
static inline void list\_replace(struct list\_head \*old,
				struct list\_head \*new)
{
	new->next = old->next;
	new->next->prev = new;
	new->prev = old->prev;
	new->prev->next = new;
}

  1. 遍历链表
/\*\*
 \* 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)


/\*\*
 \* list\_for\_each\_safe - iterate over a list safe against removal of list entry
 \* @pos: the &struct list\_head to use as a loop cursor.
 \* @n: another &struct list\_head to use as temporary storage
 \* @head: the head for your list.
 \* 使用这个方法你可以在遍历过程中删除当前遍历的元素,而不会影响接下来遍历的进行
 \*/
#define list\_for\_each\_safe(pos, n, head) \
 for (pos = (head)->next, n = pos->next; pos != (head); \
 pos = n, n = pos->next)

/\*\*
 \* list\_for\_each\_entry - iterate over list of given type
 \* @pos: the type \* to use as a loop cursor.
 \* @head: the head for your list.
 \* @member: the name of the list\_struct within the struct.
 \* 遍历list\_node成员所在的结构体类型
 \*/
#define list\_entry(ptr, type, member) \
 container\_of(ptr, type, member)

#define list\_for\_each\_entry(pos, head, member) \
 for (pos = list\_entry((head)->next, typeof(\*pos), member); \
 &pos->member != (head); \
 pos = list\_entry(pos->member.next, typeof(\*pos), member))


  1. 判断是否是尾节点,判断链表是否为空
/\*\*
 \* list\_is\_last - tests whether @list is the last entry in list @head
 \* @list: the entry to test
 \* @head: the head of the list
 \*/
static inline int list\_is\_last(const struct list\_head \*list,
				const struct list\_head \*head)
{
	return list->next == head;
}

/\*\*
 \* list\_empty - tests whether a list is empty
 \* @head: the list to test.
 \*/
static inline int list\_empty(const struct list\_head \*head)
{
	return head->next == head;
}

3. 使用案例

#include <stdio.h>

struct student{
    char\* name;
    int age;
    struct list\_head others;
};

int main(void)
{
    //学生排队,队列为lines
    LIST\_HEAD(lines);
    struct student s1 = {"s1", 10};
    struct student s2 = {"s2", 9};
    struct student s3 = {"s3", 11};


![](https://img-blog.csdnimg.cn/img_convert/9a8cb5f8c0ec69e6499adead0da6e95b.png)


最全的Linux教程,Linux从入门到精通

======================

1.  **linux从入门到精通(第2版)**

2.  **Linux系统移植**

3.  **Linux驱动开发入门与实战**

4.  **LINUX 系统移植 第2版**

5.  **Linux开源网络全栈详解 从DPDK到OpenFlow**



![华为18级工程师呕心沥血撰写3000页Linux学习笔记教程](https://img-blog.csdnimg.cn/img_convert/59742364bb1338737fe2d315a9e2ec54.png)



第一份《Linux从入门到精通》466页

====================

内容简介

====

本书是获得了很多读者好评的Linux经典畅销书**《Linux从入门到精通》的第2版**。本书第1版出版后曾经多次印刷,并被51CTO读书频道评为“最受读者喜爱的原创IT技术图书奖”。本书第﹖版以最新的Ubuntu 12.04为版本,循序渐进地向读者介绍了Linux 的基础应用、系统管理、网络应用、娱乐和办公、程序开发、服务器配置、系统安全等。本书附带1张光盘,内容为本书配套多媒体教学视频。另外,本书还为读者提供了大量的Linux学习资料和Ubuntu安装镜像文件,供读者免费下载。



![华为18级工程师呕心沥血撰写3000页Linux学习笔记教程](https://img-blog.csdnimg.cn/img_convert/9d4aefb6a92edea27b825e59aa1f2c54.png)



**本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。**

> 需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 12
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值