双向循环链表节点.结构体定义如下:
struct list_head {
struct list_head *next, *prev;
};
1 list_head定义并初始化
#define LIST_HEAD_INIT(name){ &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
所以LIST_HEAD(name)等价于
Struct list_head name = {&(name),&(name)},即:
Name的前后指针指向自己
2 list_head节点初始化
static inline voidINIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
list的前后指针指向自己
3 头部添加新节点
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline voidlist_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
4 尾部添加新节点
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specifiedhead.
* This is useful for implementing queues.
*/
static inline voidlist_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}