linux双向链表(list_head)分析

本文分析了Linux内核中用于管理数据结构的list_head双向链表。介绍了如何定义和初始化链表,以及如何进行节点的添加、删除和合并等操作。通过list_add、list_add_tail、list_splice等函数,展示了链表操作的灵活性。同时,讨论了双向链表的优势,如动态增删节点、节省存储空间和快速查找。

源码路径:include/linux/list.h
struct list_head {
    struct list_head *next, *prev;
};

 1、定义并初始化
    LIST_HEAD(A);

2、添加一个节点到表头之后(即表头的next)
    list_add(struct list_head *new, struct list_head *head)

 

3、添加一个节点到表头之前(即表头的prev,对next而言是tail)
    list_add_tail(struct list_head *new, struct list_head *head)

4、把两个链表拼接/合并
    list_splice(struct list_head *list, struct list_head *head)
    例如把链表CDE拼接到链表AB上(list_splice函数的参数head为AB链表,合并时会去掉C链表头)

5、其它操作可通过源码自行分析
    list_del (struct list_head *entry)
    list_replace (struct list_head *old,  struct list_head *new)
    list_move (struct list_head *list, struct list_head *head)
    list_splice (struct list_head *list, struct list_head *head)

6、list_head双向链表的思考
    作用:把具有相同属性或具有关联性的对象或数据链接起来方便管理
    链表:动态增减节点,相较数组可以节约存储空间
    循环:通过任意节点就可以遍历整个链表上的节点
    双向:快速定位前后节点,同时向前和向后查找可以加快查找速度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值