List链表操作

 

双向链表节点数据结构:

1 struct list_head {
2     struct list_head *next, *prev;
3 };

 

链表的初始化:

1 static inline void INIT_LIST_HEAD(struct list_head *list)
2 {
3     list->next = list;
4     list->prev = list;
5 }

 

添加一个节点:

 1 static inline void list_add(struct list_head *new, struct list_head *head)
 2 {
 3     __list_add(new, head, head->next);  // new插入到head的后面,head->next的前面
 4 }
 5 
 6 static inline void __list_add(struct list_head *new,
 7                   struct list_head *prev,
 8                   struct list_head *next)
 9 {
10     next->prev = new;
11     new->next = next;
12     new->prev = prev;
13     prev->next = new;
14 }

 

 检查链表是否为空,返回1表示空:

1 /**
2  * list_empty - tests whether a list is empty
3  * @head: the list to test.
4  */
5 static inline int list_empty(const struct list_head *head)
6 {
7     return head->next == head;
8 }

 

宏 list_for_each

功能:遍历循环双向链表。

参数head:从head节点处开始遍历循环双向链表;

参数pos: 循环变量,类似for循环的i;

1 #define list_for_each(pos, head) \
2     for (pos = (head)->next;  pos != (head); \    
3             pos = pos->next)

 

转载于:https://www.cnblogs.com/love-lp/articles/3604483.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值