脚踏实地
全部博文(230)
分类: C/C++
2011-10-07 16:55:12
struct list_head{
struct list_head *next, *prev;
};
定义一个双向链表的头节点,我们可以这样:
#define LIST_HEAD_INIT(name) { &(name), &(name) }
struct list_head head;
LIST_HEAD_INIT(head);
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.
*/
#define list_for_each(pos, head) /
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop counter.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) /
for (pos = (head)->next, n = pos->next; pos != (head); /
pos = n, n = pos->next)
删除元素之后要重新将删除了的元素进行初始化,因为他的prev和next都已经失去了意义。
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static __inline__ void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
linux源代码中的一个例子: