最新Linux内核源码中最常见的数据结构之【list_head】(1),附带学习经验

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

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

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

//创建头节点
#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};

    //将s1加入到队伍中
    list\_add\_tail(&s1.others, &lines);
    //将s2加入到队伍中
    list\_add\_tail(&s2.others, &lines);
    //将s3加入到队伍中
    list\_add\_tail(&s3.others, &lines);
    
    struct student \*cursor;
    printf("lines: \n");
    list\_for\_each\_entry(cursor, &lines, others){
        printf("%s -> ", cursor->name);
    }
    printf("\n");

    //s4这个人19岁,不讲武德,插队
    struct student s4 = {"s4", 19};
    list\_add(&s4.others, &lines);
    printf("lines: \n");
    list\_for\_each\_entry(cursor, &lines, others){
        printf("%s -> ", cursor->name);
    }
    printf("\n");

    //老师来了,把s4提出队伍


### 最后的话

最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!

### 资料预览

给大家整理的视频资料:

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

给大家整理的电子书资料:

  

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



**如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!**

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

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


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

朋友,让我有持续创作的动力!**

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

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


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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值