分析linux/list.h中的list_head结构

from: http://blog.tianya.cn/blogger/post_show.asp?BlogID=504179&PostID=5623610 

 

可以定义一套类似的对链表操作的宏,对任何链表的操作都可以用这套宏来替代,这是一个很有创意的想法,据说在C++设计与演化上讲过,书太多了,我还是慢慢来吧,:(

/usr/include/linux/list.h

struct list_head {
struct list_head *next, *prev;
};

list_head结构用于构造双向环形链表

LIST_HEAD(head) : 定义一个空表头
struct list_head head = {&head,&head};

INIT_LIST_HEAD(head) : 初始化一个已定义的表头;
head->next = head;
head->prev = head;

list_add(entry,head); 将entry添加到head之后,用于构造堆栈
head->next->prev = entry;
entry->next = head->next;
entry->prev = head;
head->next = entry;

list_add_tail(entry,head) : 将entry添加到head之前,用于构造队列
head->prev = entry;
entry->next = head;
entry->prev = head->prev;
head->prev->next = entry;

list_del(entry) : 删除entry
entry->next->prev = entry->prev;
entry->prev->next = entry->next;

list_del_init(entry) : 删除并复位entry
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
entry->next = entry;
entry->prev = entry;

list_empty(head) : 测试环形链表是否为空
(head->next == head)

list_splice(list,head) : 将两个环形链表合成一个大表
list->prev->next = head->next;
list->next->prev = head;
head->next->prev = list->prev;
head->next = list->next;

list_entry(ptr,type,member) : 如果type结构中member的地址是ptr,则返回type结构的地址
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

list_for_each(entry,head) : 遍历链表
for (entry = (head)->next; entry != (head); entry = entry->next)

你可能感兴趣的:(c,struct,list,测试,each)