list链表

一.链表头结构体

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


二.初始化链表头
也就是把list的next和prew指针指向自己

static inline void INIT_LIST_HEAD(struct list_head *list)
{
	list->next = list;
	list->prev = list;
}

三.添加链表项

通用的添加链表项函数

函数执行前

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;
}

执行后



在链表头部添加链表项

static inline void list_add(struct list_head *new, struct list_head *head)
{
	__list_add(new, head, head->next);
}

在链表尾部添加链表项

static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
	__list_add(new, head->prev, head);
}

四.删除链表项

函数执行前

static inline void __list_del(struct list_head * prev, struct list_head * next)
{
	next->prev = prev;
	prev->next = next;
}

执行后

链表中删除链表项

static inline void list_del(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);	//从链表中脱开链表项
	entry->next = LIST_POISON1;		//移除链表项的next指针
	entry->prev = LIST_POISON2;		//移除链表项的prev指针
}

五.删除链表头

static inline void list_del_init(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
	INIT_LIST_HEAD(entry);
}

六.一些list相关的宏

1.list_entry

#define list_entry(ptr, type, member) \
	container_of(ptr, type, member)

2.list_first_entry

#define list_first_entry(ptr, type, member) \
	list_entry((ptr)->next, type, member)




 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值