源码注释仓库
1. 链表结构
1.1 结点结构
链表中每个结点的结构在adlist.h
中:
typedef struct listNode {
struct listNode *prev; //指向前驱结点的指针
struct listNode *next; //指向后继结点的指针
void *value; //指向结点值的指针
} listNode;
结点的结构比较简单,没什么特殊的。
1.2 迭代器结构
typedef struct listIter {
listNode *next; //迭代器指向的结点
int direction; //迭代器方向,AL_START_HEAD 或 AL_START_TAIL
} listIter;
#define AL_START_HEAD 0
#define AL_START_TAIL 1
和c++的迭代器有点像,包括一个指向当前结点的指针,以及迭代器的方向,方向可以是AL_START_HEAD
或者AL_START_TAIL
.
AL_START_HEAD
表示从前往后(head to tail).
AL_START_TAIL
表示从后往前(tail to head).
1.3 list结构
多个结点串起来就形成了一个链表,链表包含头结点和尾结点,以及一些其他信息。
typedef struct list {
listNode *head; //头结点
listNode *tail; //尾结点
void *(*dup)(void *ptr); //结点的复制函数
void (*free)(void *ptr); //结点的释放函数
int (*match)(void *ptr, void *key);//结点的比较函数
unsigned long len; //整个链表的长度
} list;
这个结构中,使用函数指针实现类似成员函数的功能。
void *(*dup)(void *ptr);//返回一个void*的指针
1.4 一些宏定义
redis使用一些宏定义来实现简单函数的功能。
/* Functions implemented as macros */
#define listLength(l) ((l)->len) //返回链表长度
#define listFirst(l) ((l)->head) //返回链表的头结点
#define listLast(l) ((l)->tail) //返回链表的尾结点
#define listPrevNode(n) ((n)->prev) //返回当前结点的前驱
#define listNextNode(n) ((n)->next) //返回当前结点的后继
#define listNodeValue(n) ((n)->value) //返回当前结点的值
#define listSetDupMethod(l,m) ((l)->dup = (m)) //设置链表的dup函数
#define listSetFreeMethod(l,m) ((l)->free = (m)) //设置链表的free函数
#define listSetMatchMethod(l,m) ((l)->match = (m)) //设置链表的match函数
#define listGetDupMethod(l) ((l)->dup) //获取链表的dup函数
#define listGetFree(l) ((l)->free) //获取链表的free函数
#define listGetMatchMethod(l) ((l)->match) //获取链表的match函数
2. 相关操作的函数
在介绍完基本的结构之后,来看一下和链表有关的操作。
2.1 创建链表listCreate
/* Create a new list. The created list can be freed with
* AlFreeList(), but private value of every node need to be freed
* by the user before to call AlFreeList().
*
* On error, NULL is returned. Otherwise the pointer to the new list. */
list *listCreate(void)
{
struct list *list;
if ((list = zmalloc(sizeof(*list))) == NULL)//调用zmalloc
return NULL; //创建失败返回NULL
list->head = list->tail = NULL;//头尾结点均设置为NULL
list->len = 0; //长度设置为0
list->dup = NULL; //结点复制函数设置为NULL
list->free = NULL; //结点释放函数设置为NULL
list->match = NULL; //结点比较函数设置为NULL
return list;
}
该函数比较简单,就是分配内存然后将头尾结点设置为空,然后和结点相关的函数指针设置为空,返回就完事儿了。
2.2 释放整个链表listRelease
/* Free the whole list.
*
* This function can't fail. */
void listRelease(list *list)
{
listEmpty(list); //先调用listEmpty释放链表中的所有结点
zfree(list); //调用zfree释放链表
}
这个函数先调用了listEmpty
函数,listEmpty
的作用是释放链表中的每一个结点。
/* Remove all the elements from the list without destroying the list itself. */
void listEmpty(list *list)
{
unsigned long len;
listNode *current, *next;
current = list->head;
len = list->len;
while(len--) { //遍历链表中的所有结点
next = current->next;
if (list->free) list->free(current->value);//如果定义了结点的free函数,则调用free函数释放结点的值
zfree(current); //释放当前结点
current = next;
}
list->head = list->tail = NULL;//头尾结点置为NULL
list->len = 0;
}
2.3 在头部插入结点listAddNodeHead
函数原形:
list *listAddNodeHead(list *list, void *value)
成功返回list,失败返回NULL。
list *listAddNodeHead(list *list, void *value)
{
listNode *node;
if ((node = zmalloc(sizeof(*node))) == NULL)
return NULL; //内存分配失败返回NULL
node->value = value;
if (list->len == 0) {
list->head = list->tail = node;//如果链表长度为0,则将头尾结点指向新插入的结点
node->prev = node->next = NULL;//链表中只有这一个结点,前驱后继均为NULL
} else {
node->prev = NULL; //链表不为空,该结点成为链表新的头结点,前驱为NULL
node->next = list->head;//后继为原来的head
list->head->prev = node;//原来的head前驱变为新结点
list->head = node; //链表的头结点变为新结点
}
list->len++;//长度加一
return list;//返回
}
2.4 在尾部插入结点listAddNodeTail
和在头部插入结点类似。
list *listAddNodeTail(list *list, void *value)
{
listNode *node;
if ((node = zmalloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
if (list->len == 0) {
list->head = list->tail = node;
node->prev = node->next = NULL;
} else {
node->prev = list->tail;//新的尾结点的前驱 指向 旧的尾结点
node->next = NULL; //新的尾结点后继为NULL
list->tail->next = node;//旧的尾结点后继为新的尾结点
list->tail = node;
}
list->len++;
return list;
}
2.5 在某一结点前/后插入新结点listInsertNode
函数原形:
list *listInsertNode(list *list, listNode *old_node, void *value, int after)
//在old_node前后插入新结点,新结点的值为value,after为0表示在old_node前面插入,否则在old_node后面插入
//新结点内存分配失败返回NULL
list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
listNode *node;
if ((node = zmalloc(sizeof(*node))) == NULL)
return NULL; //内存分配失败返回NULL
node->value = value;
if (after) { //after非0则在old_node后面插入新结点
node->prev = old_node;
node->next = old_node->next;
if (list->tail == old_node) {
list->tail = node;//old_node为尾结点,则新的尾结点变为node
}
} else { //after为0则在old_node前面插入新结点
node->next = old_node;
node->prev = old_node->prev;
if (list->head == old_node) {
list->head = node;//old_node为头结点,则新的头结点变为node
}
}
if (node->prev != NULL) {
node->prev->next = node;
}
if (node->next != NULL) {
node->next->prev = node;
}
list->len++;
return list;
}
2.6 删除结点listDelNode
从list
中删除node
。
void listDelNode(list *list, listNode *node)
/* Remove the specified node from the specified list.
* It's up to the caller to free the private value of the node.
*
* This function can't fail. */
void listDelNode(list *list, listNode *node)
{
if (node->prev)
node->prev->next = node->next; //有前驱,前驱的next指向node的next
else
list->head = node->next; //没前驱,node为头结点,新的头结点为node->next
if (node->next)
node->next->prev = node->prev; //有后继,后继的prev指向node的prev
else
list->tail = node->prev; //没后继,node为尾结点,新的尾结点为node->prev
if (list->free) list->free(node->value);//定义了free函数则使用结点的free函数释放结点
zfree(node);
list->len--;//长度减一
}
2.7 迭代器相关操作
listIter *listGetIterator(list *list, int direction);//获取链表的迭代器
listNode *listNext(listIter *iter); //获取当前迭代器指向结点的后继结点
void listReleaseIterator(listIter *iter); //释放迭代器
2.7.1 生成迭代器listGetIterator
获取迭代器,根据direction
决定返回一个向前的迭代器还是向后的迭代器。
listIter *listGetIterator(list *list, int direction)
{
listIter *iter;
if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL;//内存分配失败返回NULL
if (direction == AL_START_HEAD)
iter->next = list->head;//返回从前向后的迭代器
else
iter->next = list->tail;//返回从后向前的迭代器
iter->direction = direction;//设置方向
return iter;
}
2.7.2 释放迭代器listReleaseIterator
没啥好说的。
/* Release the iterator memory */
void listReleaseIterator(listIter *iter) {
zfree(iter);
}
2.7.3 重置迭代器listRewind/listRewindTail
/* Create an iterator in the list private iterator structure */
void listRewind(list *list, listIter *li) {
li->next = list->head;
li->direction = AL_START_HEAD;//将li变为前向迭代器
}
void listRewindTail(list *list, listIter *li) {
li->next = list->tail;
li->direction = AL_START_TAIL;//将li变为后向迭代器
}
2.7.4 迭代器后移listNext
该函数返回当前迭代器指向的结点,并将迭代器向iter->direction
方向移动。
listNode *listNext(listIter *iter)
{
listNode *current = iter->next;//当前迭代器指向的结点
if (current != NULL) {
if (iter->direction == AL_START_HEAD)
iter->next = current->next;//前向迭代器,往后移动
else
iter->next = current->prev;//后向迭代器,往前移动
}
return current;
}
2.8 复制链表listDup
链表的整个结构中包含:头尾结点以及结点有关的函数,如dup/match/free。
复制整个链表就是对结点和结点的函数进行复制。
- 如果定义了结点的复制函数dup,则新结点为该函数的返回值(void*)。
- 如果没定义结点的复制函数,则新结点的value(void*指针)和原结点指向相同的内容。
list *listDup(list *orig)
{
list *copy; //拷贝后的链表
listIter iter; //迭代器
listNode *node;
if ((copy = listCreate()) == NULL)//内存分配失败返回NULL
return NULL;
copy->dup = orig->dup; //拷贝和结点相关的函数
copy->free = orig->free;
copy->match = orig->match;
listRewind(orig, &iter); //将迭代器指向原链表头
while((node = listNext(&iter)) != NULL) {
void *value;
if (copy->dup) {
value = copy->dup(node->value);//如果定义了dup函数则使用dup函数复制每一个结点
if (value == NULL) {
listRelease(copy);//复制整个链表期间,如果出错,释放已经复制的内容,然后返回NULL
return NULL;
}
} else
value = node->value; //没定义dup函数,新的value和旧的结点的value指向相同
if (listAddNodeTail(copy, value) == NULL) {
listRelease(copy);//将value加入链表尾部期间,如果失败,释放已经复制的内容,然后返回NULL
return NULL;
}
}
return copy;
}
2.9 查找结点listSearchKey
成功返回listNode*
失败返回NULL
.
listNode *listSearchKey(list *list, void *key)
{
listIter iter;
listNode *node;
listRewind(list, &iter); //获取迭代器
while((node = listNext(&iter)) != NULL) {
if (list->match) { //如果定义了match函数则使用match函数比较两个value
if (list->match(node->value, key)) {
return node;
}
} else { //没有定义match函数则直接比较两者是否指向同一地址
if (key == node->value) {
return node;
}
}
}
return NULL;
}
2.10 返回第i个元素
返回链表中的第index个元素。
从前往后,编号为0,1,2,3…
从后往前,编号为-1,-2,-3…
和Python类似。
支持使用负的下标,-1表示最后一个,-2表示倒数第2个。
/* Return the element at the specified zero-based index
* where 0 is the head, 1 is the element next to head
* and so on. Negative integers are used in order to count
* from the tail, -1 is the last element, -2 the penultimate
* and so on. If the index is out of range NULL is returned. */
listNode *listIndex(list *list, long index) {
listNode *n;
if (index < 0) { //index小于0,从后往前找
index = (-index)-1;
n = list->tail;
while(index-- && n) n = n->prev;
} else { //index大于0,从前往后找
n = list->head;
while(index-- && n) n = n->next;
}
return n;
}
2.10 将尾结点搬移到头部listRotate
/* Rotate the list removing the tail node and inserting it to the head. */
void listRotate(list *list) {
listNode *tail = list->tail;//链表尾结点
if (listLength(list) <= 1) return;//链表为空或只有一个结点,直接返回,没必要搬移
/* Detach current tail */
list->tail = tail->prev; //将尾结点的前驱变为新的尾结点
list->tail->next = NULL;
/* Move it as head */
list->head->prev = tail; //将尾结点变为头结点
tail->prev = NULL;
tail->next = list->head;
list->head = tail;
}
2.11 链表拼接listJoin
/* Add all the elements of the list 'o' at the end of the
* list 'l'. The list 'other' remains empty but otherwise valid. */
void listJoin(list *l, list *o) {
if (o->head)
o->head->prev = l->tail;//o链表头结点的前驱设置为l的尾结点
if (l->tail)
l->tail->next = o->head;//l不为空,l尾结点的后继设置为o的头结点
else
l->head = o->head;//l为空,l的头就是o的头
if (o->tail) l->tail = o->tail;//o不为空,l的尾结点设置为o的尾结点
l->len += o->len;//新链表的长度
/* Setup other as an empty list. */
o->head = o->tail = NULL;
o->len = 0;
}
3. 总结
双端链表算是最简单的数据结构了,通过阅读源码学到了:
1、为结构体添加函数指针实现类似成员函数的功能。
2、c实现迭代器。