前言
内核中链表是比较简单的数据结构,通过两个指针元素实现双向链表。
链表相比其他数据结构的优点是不占用连续的内存,可以动态的插入和删除节点。
比较简单的链表
链表中前后指针元素和数据元素在同一个结构体中。通过指针可以直接找到数据。
单向链表
struct list_element {
void *data;
struct list_element *next;
}
双向链表
struct list_element {
void *data;
struct list_element *next;
struct list_element *prev;
}
linux 内核中双向链表的实现
linux 内核中的链表数据结构中存放的是链表节点,而不是数据节点,通过链表指针来查找数据元素。
存放数据的结构体
struct fox {
unsigned long tail_length; /* length in centimeters of tail */
unsigned long weight; /* weight in kilograms */
bool is_fantastic; /* is this fox fantastic? */
struct list_head list; /* list of all fox structures */
};
存放链表指针的结构体
struct list_head {