Linux内核中使用的最多的是双向循环链表;
链表代码在头文件<linux/list.h>中声明,其数据结构很简单:
struct list_head {
struct list_head *next;
struct list_head *prev;
};
值得注意的是list_head通常放在自己定义的结构体中使用;
例如:
1, 定义结构体
struct stu{
int age;
char *name;
int score;
struct list_head list;
};
2, 初始化结构体及链表
struct stu *mid_stu;
mid_stu = kzmalloc(.....);
NIT_LIST_HEAD(&mid_stu->list);
3, 添加链表成员
void list_add(struct list_head *new, struct list_head *head);
4, 删除链表成员
void list_del(struct list_head *entry);
5, 遍历链表
最常用的遍历链表的方法为list_for_each_entry
遍历链表中很关键的一步是根据成员地址找到结构体地址
直接上代码:
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))