源碼註釋倉庫
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實現迭代器。