在程序開發中,經常需要用到鏈表處理數據,其具有靈活、動態增長、動態分配、動態釋放等特點。
一般在模塊化,結構化或者解耦合編程中經常需要使用它,可以讓其任務具有隊列的特點而形成生產-消費模型。
本文介紹的這種鏈表模型的單個節點(結構體)可以分爲鏈表的數據體與連接點的兩個數據塊,且鏈表連接節點需要放在鏈表節點結構體的最尾端,巧妙的利用了從結構體局部元素來獲取整個結構體的數據首地址。
1. 鏈表的結構定義
鏈表的單個節點定義如下:
struct list_node {
struct list_data data; //數據體
struct list_head node; //連接點
};
struct list_head {
struct list_head * next;
struct list_head * prev;
};
圖像結構如下:
2. 鏈表的初始化
#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);
初始化後的圖像結構:
3. 判斷鏈表是否空
/**
* 判斷list是否爲空,傳入的是鏈表的首節點(也稱爲0節點)
*/
__inline int list_empty(struct list_head *head)
{
return head->next == head;
}
4. 刪除鏈表節點
/**
* 從鏈表中刪除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;
}
5. 獲取鏈表的第一個有效節點
/**
* 獲取鏈表的第一個節點(非首節點)
*/
#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);
6. 向鏈表尾部添加節點
/**
* 在鏈表結尾添加一個節點
*/
__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);
添加一個元素的效果圖:
添加兩個節點後的效果圖:
7. 向鏈表首部添加節點
/**
* 在鏈表首部添加一個節點
*/
__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;
}
8. 遍歷鏈表的節點
/**
* 遍歷鏈表的所有節點
*/
#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)
9. 鏈表的拼接
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);
}