在程序开发中,经常需要用到链表处理数据,其具有灵活、动态增长、动态分配、动态释放等特点。
一般在模块化,结构化或者解耦合编程中经常需要使用它,可以让其任务具有队列的特点而形成生产-消费模型。
本文介绍的这种链表模型的单个节点(结构体)可以分为链表的数据体与连接点的两个数据块,且链表连接节点需要放在链表节点结构体的最尾端,巧妙的利用了从结构体局部元素来获取整个结构体的数据首地址。
链表的单个节点定义如下:
struct list_node {
struct list_data data; //数据体
struct list_head node; //连接点
};
struct list_head {
struct list_head * next;
struct list_head * prev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
INIT_LIST_HEAD与LIST_HEAD_INIT宏的效果一样,只不过一个接受结构体,一个接受结构体指针
#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)
static LIST_HEAD(m_request_list);
/**
* 判断list是否为空,传入的是链表的首节点(也称为0节点)
*/
__inline int list_empty(struct list_head *head)
{
return head->next == head;
}
/**
* 从链表中删除list的一个节点,传入需要删除的目标节点
*/
__inline void list_del(struct list_head * entry)
{
__list_del(entry->prev, entry->next);
entry->next = NULL;
entry->prev = NULL;
}
__inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
/**
* 获取链表的第一个节点(非首节点)
*/
#define list_first_entry(ptr, type, member) list_entry((ptr)->next, type, member)
//局部访问整体,也是整个链表结构的精髓所在
#define list_entry(ptr, type, member) ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
eg. struct list_node evt = list_first_entry(&m_request_list, struct list_node, node);
/**
* 在链表结尾添加一个节点
*/
__inline void list_add_tail(struct list_head * newitem, struct list_head * head)
{
__list_add(newitem, head->prev, head);
}
__inline void __list_add(struct list_head * newitem,
struct list_head * prev, struct list_head * next)
{
next->prev = newitem;
newitem->next = next;
newitem->prev = prev;
prev->next = newitem;
}
eg. struct list_node * olditem;
list_add_tail(&(olditem->node), &m_request_list);
添加一个元素的效果图:
添加两个节点后的效果图:
/**
* 在链表首部添加一个节点
*/
__inline void list_add(struct list_head * newitem,
struct list_head * head)
{
__list_add(newitem, head, head->next);
}
__inline void __list_add(struct list_head * newitem,
struct list_head * prev, struct list_head * next)
{
next->prev = newitem;
newitem->next = next;
newitem->prev = prev;
prev->next = newitem;
}
/**
* 遍历链表的所有节点
*/
#define list_for_each_entry(pos, head, member, type) \
for (pos = list_entry((head)->next, type, member); &pos->member != (head); pos = list_entry(pos->member.next, type, member))
/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop counter.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member, type) \
for (pos = list_entry((head)->next, type, member), \
n = list_entry(pos->member.next, type, member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, type, member))
eg. struct list_node * i;
list_for_each_entry(i, &m_request_list, node, struct list_node)
static __inline void __list_splice(struct list_head * list,
struct list_head * head)
{
struct list_head * first = list->next;
struct list_head * last = list->prev;
struct list_head * at = head->next;
first->prev = head;
head->next = first;
last->next = at;
at->prev = last;
}
/**
* list_splice - join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static __inline void list_splice(struct list_head * list,
struct list_head * head)
{
if (!list_empty(list))
__list_splice(list, head);
}