一、单链表的局限性
(1) 单链表各个节点之间只由一个指针单向链接,这样实现有一些局限性。局限性主要体现在单链表只能经由指针单向移动(一旦指针移动过某个节点就无法再回来,如果要再次操作这个节点除非从头指针开始再次遍历一次),因此单链表的单向移动性导致我们在操作单链表时,当前节点只能向后移动不能向前移动,因此不自由,不利于解决更复杂的算法。
(2) 解决思路:
双向链表的节点 = 有效数据 + 2个指针(一个指向后一个节点,另一个指向前一个节点)
二、双链表
下面代码演示了双链表的创建和添加节点及其前后遍历等
#include <stdio.h>
#include <stdlib.h>
// 双链表的节点
struct node
{
int data; // 有效数据
struct node *pPrev; // 前向指针,指向前一个节点
struct node *pNext; // 后向指针,指向后一个节点
};
struct node *create_node(int data)
{
struct node *p = (struct node *)malloc(sizeof(struct node));
if (NULL == p)
{
printf("malloc error.\n");
return NULL;
}
p->data = data;
p->pPrev = NULL;
p->pNext = NULL; // 默认创建的节点前向后向指针都指向NULL
return p;
}
// 将新节点new插入到链表pH的尾部
void insert_tail(struct node *pH, struct node *new)
{
// 第一步先走到链表的尾节点
struct node *p = pH;
while (NULL != p->pNext)
{
p = p->pNext; // 第一次循环走过了头节点
}
// 循环结束后p就指向了原来的最后一个节点
// 第二步:将新节点插入到原来的尾节点的后面
p->pNext = new; // 后向指针关联好了。新节点的地址和前节点的next
new->pPrev = p; // 前向指针关联好了。新节点的prev和前节点的地址
// 前节点的prev和新节点的next指针未变动
}
// 将新节点new前插入链表pH中。
// 算法参照图示进行连接,一共有4个指针需要赋值。注意的是顺序。
void insert_head(struct node *pH, struct node *new)
{
// 新节点的next指针指向原来的第1个有效节点的地址
new->pNext = pH->pNext;
// 原来第1个有效节点的prev指针指向新节点的地址
if (NULL != pH->pNext)//判断刚开始插入的节点
pH->pNext->pPrev = new;
// 头节点的next指针指向新节点地址
pH->pNext = new;
// 新节点的prev指针指向头节点的地址
new->pPrev = pH;
}
int main(void)
{
struct node *pHeader = create_node(0); // 头指针
//insert_tail(pHeader, create_node(11));
//insert_tail(pHeader, create_node(12));
//insert_tail(pHeader, create_node(13));
insert_head(pHeader, create_node(11));
insert_head(pHeader, create_node(12));
insert_head(pHeader, create_node(13));
// 遍历
printf("node 1 data: %d.\n", pHeader->pNext->data);
printf("node 2 data: %d.\n", pHeader->pNext->pNext->data);
printf("node 3 data: %d.\n", pHeader->pNext->pNext->pNext->data);
struct node *p = pHeader->pNext->pNext->pNext; // p指向了最后一个节点
printf("node 3 data: %d.\n", p->data);
printf("node 2 data: %d.\n", p->pPrev->data);
printf("node 1 data: %d.\n", p->pPrev->pPrev->data);
return 0;
}
三、linux内核链表
内核中核心纯链表的实现在list.h文件中,list.h中就是一个纯链表的完整封装,包含节点定义和各种链表操作方法。
/usr/src/linux-headers-4.15.0-45/include/linux/list.h(ubuntu16.04)
调用其它头文件外,首先看到的是两个宏定义:
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
对宏定义进行替换之后,可以得到:
#define LIST_HEAD(name) struct list_head name = { &(name), &(name) }
即该宏定义就是定义一个名为name的结构体变量,并将变量中两个成员(前向指针和后向指针)初始化为自身地址。
list_head结构体类型在 /usr/src/linux-headers-4.15.0-45/include/linux/types.h中定义:
struct list_head {
struct list_head *next, *prev;
};
可以看出,该结构体算不上一个链表的节点,仅仅是定义了数据域,而没有定义指针域。
(1)、链表的添加
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
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);
}
该内核函数实现的功能是在prev节点和next节点之间添加一个new节点,如下图所示:
以下是对__list_add_valid()函数的分析:
#ifdef CONFIG_DEBUG_LIST
extern bool __list_add_valid(struct list_head *new,
struct list_head *prev,
struct list_head *next);
extern bool __list_del_entry_valid(struct list_head *entry);
#else
static inline bool __list_add_valid(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
return true;
}
static inline bool __list_del_entry_valid(struct list_head *entry)
{
return true;
}
#endif
当定义了CONFIG_DEBUG_LIST宏时,该函数仅仅是一个extern声明,说明函数实体不在本文件中定义,具体定义来源暂时没有找到,欢迎大佬指点;当没有定义是,该函数直接返回true。
以下是对WRITE_ONCE宏定义的分析:
WRITE_ONCE()与READ_ONCE()同属于ACCESS_ONCE()宏,WRITE_ONCE (a,b)其实就等价于a=b,READ_ONCE(x)即读取x的值,但是为什么不直接使用呢?由于C语言本身没有并发的概念,但实际情况中并发是不可避免的,ACCESS_ONCE()就是为解决这一问题存在的,该宏确保作为参数传递的值仅由生成的代码访问一次。通俗的讲,如果直接赋值a=b;b的值随时可能被其他进程修改,这样得到的A值是不可靠的,那么ACCESS_ONCE()做了什么呢?先取x的地址,将其转换为x类型的指针,将其强制转换为x类型的volatile(在C中,提醒编译器后面定义的变量随时会改变,需要读取变量时直接从变量地址进行读取)指针,再指向该指针。即将相关变量的类型转为volatile类型,直接对内存地址进行访问。这样成功避免了多线程并发导致变量被修改的问题,在共享变量未上锁的情况下,ACCESS_ONCE()为我们提供了确保读取有效数据的方法。
综合上述分析,__list_add()实现分为四步,如下图所示:
在实现了__list_add()函数的基础之上,对该函数进行简单地封装,分别形成了头插入和尾插入两种方式:
/**
* 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);
}
此外内核链表还实现了好多双链表的操作,如节点替换,节点删除等等,再此不一一进行分析
ref:
朱有鹏课堂笔记
https://blog.csdn.net/u013246898/article/details/52862290
https://blog.csdn.net/weixin_43122409/article/details/82831268