【文件属性】:
文件名称:C语言的单链表
文件大小:7KB
文件格式:ZIP
更新时间:2022-04-21 07:11:38
LINUX C/C
二.内核链表
内核链表是一种链表,Linux内核中的链表都是用这种形式实现的
1.特性
内核链表是一种双向循环链表,内核链表的节点节点结构中只有指针域
使用内核链表的时候,将内核链表作为一个成员放入到一个结构体中使用
我们在链表中找到内核链表结构的地址,通过这个地址就可以找到外部大结构体的地址,通过大结构体就可以访问其中的成员
优势:
内核链表突破了保存数据的限制,可以用内核链表来保存任何数据(使用一种链表表示各种类型的数据,通用性很强)
内核链表中只有指针域,维护起来更加方便,效率更高
2.使用
内核链表在内核中已经被实现,我们只需要调用其接口直接使用即可
内核链表的实现代码在内核源代码的list.h文件中
3.源代码分析
(1)节点结构:
struct list_head {
struct list_head *next, *prev;//前置指针 后置指针
};
(2)初始化
#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)
(3)插入
//从头部插入
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);
}
(4)通过节点找到外部结构体的地址
//返回外部结构体的地址,第一个参数是节点地址,第二个参数是外部结构体的类型名,第三个参数是节点在外部结构体中的成员名
#define list_entry(ptr, type, member)
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
(5)遍历内核链表
//遍历内核链表
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); \
pos = pos->next)
//安全遍历内核链表
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
二.内核链表
内核链表是一种链表,Linux内核中的链表都是用这种形式实现的
1.特性
内核链表是一种双向循环链表,内核链表的节点节点结构中只有指针域
使用内核链表的时候,将内核链表作为一个成员放入到一个结构体中使用
我们在链表中找到内核链表结构的地址,通过这个地址就可以找到外部大结构体的地址,通过大结构体就可以访问其中的成员
优势:
内核链表突破了保存数据的限制,可以用内核链表来保存任何数据(使用一种链表表示各种类型的数据,通用性很强)
内核链表中只有指针域,维护起来更加方便,效率更高
2.使用
内核链表在内核中已经被实现,我们只需要调用其接口直接使用即可
内核链表的实现代码在内核源代码的list.h文件中
3.源代码分析
(1)节点结构:
struct list_head {
struct list_head *next, *prev;//前置指针 后置指针
};
(2)初始化
#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)
(3)插入
//从头部插入
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);
}
(4)通过节点找到外部结构体的地址
//返回外部结构体的地址,第一个参数是节点地址,第二个参数是外部结构体的类型名,第三个参数是节点在外部结构体中的成员名
#define list_entry(ptr, type, member)
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
(5)遍历内核链表
//遍历内核链表
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); \
pos = pos->next)
//安全遍历内核链表
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
二.内核链表
内核链表是一种链表,Linux内核中的链表都是用这种形式实现的
1.特性
内核链表是一种双向循环链表,内核链表的节点节点结构中只有指针域
使用内核链表的时候,将内核链表作为一个成员放入到一个结构体中使用
我们在链表中找到内核链表结构的地址,通过这个地址就可以找到外部大结构体的地址,通过大结构体就可以访问其中的成员
优势:
内核链表突破了保存数据的限制,可以用内核链表来保存任何数据(使用一种链表表示各种类型的数据,通用性很强)
内核链表中只有指针域,维护起来更加方便,效率更高
2.使用
内核链表在内核中已经被实现,我们只需要调用其接口直接使用即可
内核链表的实现代码在内核源代码的list.h文件中
3.源代码分析
(1)节点结构:
struct list_head {
struct list_head *next, *prev;//前置指针 后置指针
};
(2)初始化
#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)
(3)插入
//从头部插入
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);
}
(4)通过节点找到外部结构体的地址
//返回外部结构体的地址,第一个参数是节点地址,第二个参数是外部结构体的类型名,第三个参数是节点在外部结构体中的成员名
#define list_entry(ptr, type, member)
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
(5)遍历内核链表
//遍历内核链表
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); \
pos = pos->next)
//安全遍历内核链表
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
C语言下的单链表,可以增加,删除,查找,销毁节点。
【文件预览】:
linklist
----linklist.o(2KB)
----main.o(2KB)
----linklist.c(2KB)
----Makefile(160B)
----linklist.h(556B)
----linklist(7KB)
----main.c(625B)