当前位置:网站首页>【Redis底层解析】链表类型
【Redis底层解析】链表类型
2022-07-25 21:34:00 【小生凡一】
链表
链表提供了高效的节点重排能力,以及顺序性的节点访问方式,并且可以通过增删节点来灵活调整链表的长度。
链表在redis中也被广泛运用,比如在发布与订阅,慢查询,监视器等功能中也用到了链表。本身还使用链表来保存多个客户端的状态信息,以及使用链表来构建客户端输出缓冲区(output buffer)。
1. 链表和链表节点的实现
每个链表节点使用一个 adlist.h/listNode结构来表示:
typedef struct listNode{
struct listNode *prev; // 前置节点
struct listNode *next; // 后置节点
void *value; // 节点的值
}listNode;
多个 listNode 可以通过 prev 和 next 指针组成双端链表
如下图所示
虽然仅仅使用多个listNode结构就可以组成链表,但是用adlist.h/list来持有链表的话,操作起来会更方便。
typedef struct list{
listNode *head; // 表头节点
listNode *tail; // 表尾节点
unsigned long len; // 链表所包含的节点数量
void *(*dup)(void *ptr); // 节点值复制函数
void *(*free)(void *ptr); // 节点值释放函数
int (*match)(void *ptr,void *key); // 节点值对比函数
}list;
list 结构为链表提供了表头指针 head,表尾指针 tail,以及链表长度计数器 len。
dup,free 和match 成员则是用于实现多态链表所需要的类型特定函数。
- dup 用于复制链表节点所保存的值
- free 函数用于释放链表节点所保存的值
- match 函数则用于对比链表节点所保持的值和另一个输入值是否相等

2. 链表的特点
- 双端:链表节点带有
prev和next指针,获取某个节点的前置节点和后置节点的复杂度都是O(1) - 无环:表头节点的
prev指针和表尾节点的next指针都指向NULL,对链表的访问以NULL为终点。 - 带表头指针和表尾指针:通过
list结构和head指针和tail指针,程序获取链表的表头节点和表尾节点的复杂度为O(1) - 带由链表长度计数器:程序使用list结构的
len属性来对list持有的链表节点进行计数,程序获取链表中的节点数量复杂度也为O(1) - 多态:链表节点使用
void*指针来保存节点值,并且可以通过list结构的dup,free,match三个属性为节点值设置类型特定函数,所以链表可以用于保存各种不同类型的值。
边栏推荐
- Airtest solves the problem that a password needs to be entered in the process of "automatic packaging" (the same applies to random bullet frame processing)
- When facing complex problems, systematic thinking helps you understand the essence of the problem
- 【面试:并发篇24:多线程:综合练习】顺序控制
- How to store pictures in the database "suggested collection"
- [database] index
- Oracle RAC RMAN backup error ora-19501 ora-15081
- Stm3 (cubeide) lighting experiment
- YUV422 to RGB (422sp to 420p)
- 全志芯片bsp命名规则
- PE格式: 分析IatHook并实现
猜你喜欢

My heart's broken! After being cheated by 30000, a 16-year-old girl was unconvinced and cheated by 50000

pyqt5使用pyqtgraph绘制多个Y值散点图

五、品达通用权限系统__pd-tools-xxs(防跨站脚本攻击)

DDD的Go实战

Zero basic learning canoe panel (17) -- panel CAPL function
![[database] conceptual design, logical design, relational database design theory](/img/4d/be7ab21c98fc1bf4b63e4abe22d9fc.png)
[database] conceptual design, logical design, relational database design theory
![[ManageEngine] value brought by Siem to enterprises](/img/1e/56d64d193e0428523418bef5e98866.png)
[ManageEngine] value brought by Siem to enterprises

Advanced technology management - how can the team be broken?

作为测试,如何理解线程同步异步

Babbitt | metauniverse daily must read: the popularity of virtual people has decreased, and some "debut is the peak", and the onlookers have dispersed
随机推荐
How to solve the problem of high concurrency and large traffic with PHP
cts测试步骤(卡西欧cts200测试)
图片怎么存储到数据库里「建议收藏」
DDD的Go实战
Unity Metaverse(二)、Mixamo & Animator 混合树与动画融合
我也是醉了,Eureka 延迟注册还有这个坑!
PE格式: 分析IatHook并实现
Rent two or three things
How to automatically generate short chains? How to generate links with UTM parameters online in batches?
Mysql8.0 MHA to achieve high availability "MHA"
CTS test steps (Casio cts200 test)
Interface testing tool restlet client
The noise reduction effect is increased by more than 6 times! With the microphone inside the headset, this wireless noise reduction headset is unusual!
Zero basic learning canoe panel (17) -- panel CAPL function
Airtest解决“自动装包”过程中需要输入密码的问题(同适用于随机弹框处理)
工作面试总遇秒杀? 看了京东 T8 大咖私藏的秒杀系统笔记, 已献出膝盖
租房二三事
Job interviews are always a second kill? After reading the seckill system notes secretly stored by JD T8, I have given my knees
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
QT | learn about QT creator by creating a simple project