Linux- struct list_head简介( 二 )


struct qingmu mu1;mu1.age = 19;mu1.weight = 61;list_add(&mu1,&mu->list);
这样我们就可以把mu1这个结点插入到mu结点的链表中去了 。
void list_add_tail(struct list_head *new, struct list_head *head){__list_add(new, head->prev, head);}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;}
从上面的函数可以看出,把新结点(new)插入到head结点前面 。
例:
struct qingmu mu2;mu2.age = 20;mu2.weight = 62;list_add(&mu2,&mu->list);
从链表中删除一个结点
从列表中删除一个结点:
static inline void list_del(struct list_head *entry){__list_del(entry->prev, entry->next);entry->next = LIST_POISON1;entry->prev = LIST_POISON2;}static inline void __list_del(struct list_head * prev, struct list_head * next){next->prev = prev;prev->next = next;}
链表删除函数,传入参数是要删除的节点,调用函数,完成删除,删除后的节点的指针赋值为和宏,完成删除 。和定义在 /linux/list.h文件
#define LIST_POISON1((void *) 0x00100100)#define LIST_POISON2((void *) 0x00200200)
删除后,该选节点已不在链表当中,因此不会再使用 。/2是两个不会存在于内核空间的地址,如果使用,就会报错 。因此,设置指针,是强制禁用该节点 。
利用(*entry) 接口就可以删除链表中的任意节点了,但需注意,前提条件是这个节点是已知的,既在链表中真实存在,切prev,next指针都不为NULL 。
例:
list_del(&mu2.list);
判断链表是否为空
static inline int list_empty(const struct list_head *head){return head->next == head;}
通过判断传入结点的 下一个结点是否是自己来判断链表是否为空 。
遍历链表
正向遍历:
#define list_for_each(pos, head) \for (pos = (head)->next; prefetch(pos->next), pos != (head); \pos = pos->next)#define prefetch(x) __builtin_prefetch(x)
一个简单的for循环 。
循环的初始化工作:pos指向链表头的下一项 。
循环的条件:pos不是链表头 。
每次循环要做的事情:pos指向链表中的下一项
反向遍历:
#define list_for_each_prev(pos, head) \for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \pos = pos->prev)
例:
list_for_each(p,list_head){......}
例 list.h
#ifndef LIST_H#define LIST_H#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)#define container_of(ptr, type, member) ({ \const typeof( ((type *)0)->member ) *__mptr = (ptr); \(type *)( (char *)__mptr - offsetof(type,member) );})#define prefetch(x) __builtin_prefetch(x)struct list_head {struct list_head *next, *prev;};typedef struct qingmu{unsigned int age;unsigned int weight;struct list_head list;}Qingmu_t;//静态初始化#define LIST_HEAD_INIT(name) { &(name), &(name) }//链表头#define LIST_HEAD(name) \struct list_head name = LIST_HEAD_INIT(name)#define list_for_each(pos, head) \for (pos = (head)->next; prefetch(pos->next), pos != (head); \pos = pos->next)#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); \prefetch(pos->member.next), &pos->member != (head);\pos = list_entry(pos->member.next, typeof(*pos), member))void INIT_LIST_HEAD(struct list_head *list);void __list_add(struct list_head *new,struct list_head *prev,struct list_head *next);void list_add_tail(struct list_head *new, struct list_head *head);void list_add(struct list_head *new, struct list_head *head);void __list_del(struct list_head * prev, struct list_head * next);void list_del(struct list_head *entry);int list_empty(const struct list_head *head);#endif