操作
定义
内核中定义的双向循环链表 /include/linux/types.h
struct list_head {
struct list_head *prev, *next;
};
声明和初始化
声明:
#define LIST_HEAD_INIT(name) { &(name), &(name) }
声明并初始化:
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
插入
从头部插入
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;
}
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
从尾部插入
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
删除
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
遍历
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @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 cursor.
* @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)
/**
* 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.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, 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 cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member), \
n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
实验
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "list.h"
struct num {
int i;
struct list_head list;
};
int main(int argc, char **argv) {
LIST_HEAD(test_list);
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);
}
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;
}