linux-链表及相关链表操作

1.链表结构体

struct list_head{
	struct list_head *next;
	stuct list_head *prev;
}

2.list_entry

#define container_of(ptr, type, member)\
	container_of(ptr, type, member)
//ptr为list_head指针,type为包含list_head结构体对象类型,
//member为链表结构体变量名

3.链表初始化

INIT_LIST_HEAD()

#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)
{
	WRITE_ONCE(list->next, list);
	list->prev = list;
}

4.链表操作

list_add(sturct list_head *new, struct list_head *head)
//head 前加入new( 栈)

list_add_tail(sturct list_head *new, struct list_head *head)
//head后加入new(队列)

list_del(struct list_head *entry)
//不删除entry数据结构

list_move(struct list_head *list, struct list_head *head)
//list移除加到head后面

list_move_tail(struct list_head *list, struct list_head *head)
//list移除加到head前面

list_empty(struct list_head *head)
//链表是否为空

list_splice(struct list_head *list, struct list_head *head)
//连接两个链表

5.遍历链表

list_for_each(pos,head)
//pos遍历当前position,head链表头

list_for_each_entry(pos, head, member)
//pos为包含list_head节点对象的指针,head为头节点指针,member为pos中list_head结构变
//量名

list_for_each_entry_reverse(pos, head, member)
//反向遍历

list_for_each_entry_safe(pos, next, head, member)
list_for_each_entry_safe_reverse(pos, next, head, member)
//next与pos类型一致
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值