链表操作与管理

在程序开发中,经常需要用到链表处理数据,其具有灵活、动态增长、动态分配、动态释放等特点。
一般在模块化,结构化或者解耦合编程中经常需要使用它,可以让其任务具有队列的特点而形成生产-消费模型。
本文介绍的这种链表模型的单个节点(结构体)可以分为链表的数据体与连接点的两个数据块,且链表连接节点需要放在链表节点结构体的最尾端,巧妙的利用了从结构体局部元素来获取整个结构体的数据首地址。

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_HEADLIST_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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章