1.链表的初始化
-
#define INIT_LIST_HEAD(ptr) do { \
-
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
-
} while (0)
初始化后形成一个带头结点的链表,如下所示

1.1 判断链表是否为空
-
static __inline__ int list_empty(struct list_head *head)
-
{
-
return head->next == head;
-
}
由head->next是不是指向head本身来判断链表是不是为空
2.链表的插入
2.1 有两种方式进行链表的插入 list_add与list_add_tail
-
static __inline__ void list_add(struct list_head *new, struct list_head *head)
-
{
-
__list_add(new, head, head->next);
-
}
下面是list_add
插入结点:
注意这儿不需要malloc,结点指针必须在其它地方分配好

2.2 list_add_tail
-
static __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
-
{
-
__list_add(new, head->prev, head);
-
}
下面是list_add_tail插入结点
:注意这儿不需要malloc,结点指针必须在其它地方分配好

2.3 他们都调用了
__list_add进入指针的操作
-
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;
-
}
3.链表的删除
3.1 调用list_del进行结点的删除,注意
参数entry就是要删除的结点,
-
static __inline__ void list_del(struct list_head *entry)
-
{
-
__list_del(entry->prev, entry->next);
-
}
例如要删除node2,则参数entry=node2,list_del分别修改node3与node1的next与prev指针把node2从链表中删除

虽然head->next->next 或者head->prev->prev搜索链表时找不到node2了,表面上看node2是被删掉,
但是node2本身的prev与next还分别指向node3与node1
3.2
调用list_del_init进行结点的安全删除,注意参数entry就是要删除的结点,
并使删除后的node指向自己本身
-
static __inline__ void list_del_init(struct list_head *entry)
-
{
-
__list_del(entry->prev, entry->next);
-
INIT_LIST_HEAD(entry);
-
}
阅读(1454) | 评论(0) | 转发(0) |