linux 内核提供的链表接口函数定义在 include/linux/list.h 中
main.c
struct num { // 封装的自己的数据结构
int i;
struct list_head list; // 将链表的操作以及组织完全交由内核提供的工具
};
int main(int argc, char **argv) {
LIST_HEAD(test_list); // 链表头结点 test_list
/*
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
*/
int i;
struct num *num; // 声明变量
struct num *num1;
srand((unsigned)time(NULL));
/* Add */
printf("add 100 element into list\n");
for (i = 0; i < 100; i++) {
num = (struct num*)malloc(sizeof(struct num)); // 创建了一个节点
num->i = i;
list_add_tail(&num->list, &test_list); // new entry to be added,list head to add it before 头插法
}
i = 0;
/* print list */
printf("printf the list\n");
list_for_each_entry(num, &test_list, list) {
printf("%2d ", num->i);
if ((i+1)%10 == 0)
printf("\n");
i++;
}
printf("\n");
/* Delete */
list_for_each_entry_safe(num, num1, &test_list, list) {
list_del(&num->list);
free(num);
}
if (list_empty(&test_list))
printf("Free test_list successfully\n");
return 0;
}
list.h
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
if (!__list_add_valid(new, prev, next))
return;
next->prev = new;
new->next = next;
new->prev = prev;
WRITE_ONCE(prev->next, new);
}
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
static inline void list_add_tail(struct list_head *new, struct list_head *head) // 新节点,头结点
{
__list_add(new, head->prev, head);
}
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
WRITE_ONCE(prev->next, next);
}
static inline void __list_del_entry(struct list_head *entry)
{
if (!__list_del_entry_valid(entry))
return;
__list_del(entry->prev, entry->next);
}
static inline void list_del(struct list_head *entry)
{
__list_del_entry(entry);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head)
{
return READ_ONCE(head->next) == head;
}