1、数据结构
struct list_head
{
struct list_head *next, *prev;
};
这里用一个list_head类型的结构体,它包含两个指向本身的指针prev和next,从而具备了双向链表的功能。与一般双向链表不同的是,该结构没有数据域。而它一般也是作为我们实际运用的双向链表中一个数据成员。例如:
struct my_struct
{
struct list_head list;
unsigned long num;
char name[20];
};
2、创建list_head结构
链表在使用前是要初始化的,而大多数情况下,链表都是动态创建的,所以一般就是在运行时初始化。
通常使用两种方法:
1.显式初始化
struct list_head example_list;
INIT_LIST_HEAD(&example_list);
2.隐式初始化
LIST_HEAD(example_list);
3、API接口
(1)初始化
#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)
{
list->next = list;
list->prev = list;
}
(2)节点插入
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);
}
(3)节点删除
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;
entry->prev = LIST_POISON2;
}
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
(4)节点替换
static inline void list_replace(struct list_head *old,
struct list_head *new)
{
new->next = old->next;
new->next->prev = new;
new->prev = old->prev;
new->prev->next = new;
}
static inline void list_replace_init(struct list_head *old,
struct list_head *new)
{
list_replace(old, new);
INIT_LIST_HEAD(old);
}
(5)节点转移(从一个链上删除之后添加到新链中)
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
(6)判断节点是否是尾节点
static inline int list_is_last(const struct list_head *list,
const struct list_head *head)
{
return list->next == head;
}
(7)判断链表是否为空
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
static inline int list_empty_careful(const struct list_head *head)
{
struct list_head *next = head->next;
return (next == head) && (next == head->prev);
}
描述:
tests whether a list is empty _and_ checks that no other CPU might be
in the process of modifying either member (next or prev)
(8)旋转链表的第一个节点到最后
static inline void list_rotate_left(struct list_head *head)
{
struct list_head *first;
if (!list_empty(head)) {
first = head->next;
list_move_tail(first, head);
}
}
(9)判断链表是否只有一个节点
static inline int list_is_singular(const struct list_head *head)
{
return !list_empty(head) && (head->next == head->prev);
}
(10)分割链表
static inline void __list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry)
{
struct list_head *new_first = entry->next;
list->next = head->next;
list->next->prev = list;
list->prev = entry;
entry->next = list;
head->next = new_first;
new_first->prev = head;
}
static inline void list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry)
{
if (list_empty(head))
return;
if (list_is_singular(head) &&
(head->next != entry && head != entry))
return;
if (entry == head)
INIT_LIST_HEAD(list);
else
__list_cut_position(list, head, entry);
}
注:
list:将剪切的结点要加进来的链表
head:被剪切的链表
entry:所指位于由head所指领头的链表内,它可以指向head,但是这样的话,head就不能被剪切了,在代码中调用了INIT_LIST_HEAD(list)。是将head(不包括head)到entry之间的所有结点剪切下来加到list所指向的链表中。这个操作之后就有了两个链表head和list。
(11)链表合并
static inline void __list_splice(const struct list_head *list,
struct list_head *prev,
struct list_head *next)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
栈设计
static inline void list_splice(const struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head, head->next);
}
队列设计
static inline void list_splice_tail(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head->prev, head);
}
合并两个链表后初始化空链表
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head, head->next);
INIT_LIST_HEAD(list);
}
}
static inline void list_splice_tail_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head->prev, head);
INIT_LIST_HEAD(list);
}
}
(12)container_of宏获取type类型结构体的起始指针
#define container_of(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
参数
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
(13)获取第一个节点元素
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
#define list_first_entry_or_null(ptr, type, member) \
(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
(14)获取指定pos的下一个节点元素
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
(14)获取指定pos的上一个节点元素
#define list_prev_entry(pos, member) \
list_entry((pos)->member.prev, typeof(*(pos)), member)
(15)遍历链表
正向遍历
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
注意:此宏必要把list_head放在数据结构第一项成员,至此,它的地址也就是结构变量的地址。
反向遍历
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
安全遍历
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
#define list_for_each_prev_safe(pos, n, head) \
for (pos = (head)->prev, n = pos->prev; \
pos != (head); \
pos = n, n = pos->prev)
从head节点开始(不包括head节点!)遍历它的每一个节点!它用n先将下一个要遍历的节点保存起来,防止删除本节点后,无法找到下一个节点,而出现错误!
(16)循环遍历每一个pos中的member子项
正向遍历
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
反向遍历
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_entry((head)->prev, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.prev, typeof(*pos), member))
判断pos这个指针是否为空,为空的话给它赋值list_entry(head, typeof(*pos), member)这条语句求出来的结构体的地址!
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, typeof(*pos), member))
已知指向某个结构体的指针pos,以及指向它中的member成员的head指针,从它的下一个结构体开始向后遍历这个链表。
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
已知指向某个结构体的指针pos,以及指向它中的member成员的head指针,从它的前一个结构体开始向前遍历这个链表。
#define list_for_each_entry_continue_reverse(pos, head, member) \
for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.prev, typeof(*pos), member))
从pos节点开始,向后遍历链表。
#define list_for_each_entry_from(pos, head, member) \
for (; &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
先保存下一个要遍历的节点!从head下一个节点向后遍历链表。
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
先保存下一个要遍历的节点!从pos下一个节点向后遍历链表。
#define list_for_each_entry_safe_continue(pos, n, head, member) \
for (pos = list_entry(pos->member.next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
先保存下一个要遍历的节点!从pos节点向后遍历链表。
#define list_for_each_entry_safe_from(pos, n, head, member) \
for (n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
先保存下一个要遍历的节点!从链表尾部向前遍历链表。
#define list_for_each_entry_safe_reverse(pos, n, head, member) \
for (pos = list_entry((head)->prev, typeof(*pos), member), \
n = list_entry(pos->member.prev, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.prev, typeof(*n), member))
获取n结构体指针
#define list_safe_reset_next(pos, n, member) \
n = list_entry(pos->member.next, typeof(*pos), member)
4、list_for_each和list_for_each_entry实例
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "list.h"
typedef struct Node
{
struct list_head node;
int a;
int b;
}NODE;
typedef struct Head
{
struct list_head list;
int count;
}HEAD;
HEAD ghead;
int main(int argc,char **argv)
{
struct list_head *pos=NULL;
struct list_head *head=NULL;
NODE *pNode = NULL;
NODE *pPosNode = NULL;
int i = 0;
INIT_LIST_HEAD(&(ghead.list));
ghead.count=0;
for(i=0;i<10;i++)
{
pNode = (NODE *)malloc(sizeof(NODE));
if(!pNode)
{
return -1;
}
memset(pNode,0,sizeof(NODE));
pNode->a=i+1;
pNode->b=i+2;
//list_add(&pNode->node,&(ghead.list));
list_add_tail(&pNode->node,&(ghead.list));
}
head = &(ghead.list);
list_for_each(pos,head)
{
pNode = list_entry(pos,NODE,node);
printf("list_for_each %d-%d\n",pNode->a,pNode->b);
pNode = NULL;
}
list_for_each_entry(pPosNode,&(ghead.list),node)
{
printf("list_for_each_entry %d-%d\n",pPosNode->a,pPosNode->b);
}
return 0;
}
执行结果
list_for_each 1-2
list_for_each 2-3
list_for_each 3-4
list_for_each 4-5
list_for_each 5-6
list_for_each 6-7
list_for_each 7-8
list_for_each 8-9
list_for_each 9-10
list_for_each 10-11
list_for_each_entry 1-2
list_for_each_entry 2-3
list_for_each_entry 3-4
list_for_each_entry 4-5
list_for_each_entry 5-6
list_for_each_entry 6-7
list_for_each_entry 7-8
list_for_each_entry 8-9
list_for_each_entry 9-10
list_for_each_entry 10-11
5.内核list.h源文件
list.h