介绍
Redis自己实现了一个含有迭代器的双向链表。基本功能就是通用的双向链表,源码实现还是值得阅读以下的。
源文件
adlist.h adlist.c
分析
这里主要介绍其主要的数据结构其他的链表相关的操作有兴趣的话可以自己去看源码,其中的细节tricky还是挺多的。
- /* Node, List, and Iterator are the only data structures used currently. */
- /*
- * 链表节点
- */
- typedef struct listNode {
- // 前驱节点
- struct listNode *prev;
- // 后继节点
- struct listNode *next;
- // 值
- void *value;
- } listNode;
- /*
- * 链表迭代器
- */
- typedef struct listIter {
- // 下一节点
- listNode *next;
- // 迭代方向
- int direction;
- } listIter;
- /*
- * 链表
- */
- 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;