1. 程式人生 > 資料庫 >redis6.0原始碼學習(三)adlist

redis6.0原始碼學習(三)adlist

redis6.0原始碼學習(三)adlist

文章目錄

1、資料結構

下面是adlist主要結構體:

typedef struct listNode {
    struct listNode *prev; //prev指標,指向前一個節點
    struct listNode *next;//next指標,指向下一個節點
    void *value;
} listNode;
//迭代器
typedef struct listIter {
    listNode *next;
    int direction; //迭代器方向
} listIter;

typedef struct list {
    listNode *head; //head指標指向連結串列頭部
    listNode *tail; //head指標指向連結串列頭部
    void *(*dup)(void *ptr); //自定義節點值的複製函式,如果不定義,預設策略的複製操作會讓原連結串列和新連結串列共享同一個資料域
    void (*free)(void *ptr);//自定義節點free操作 
    int (*match)(void *ptr, void *key);//search操作的時候比較兩個value是否相等,預設策略是比較兩個指標的值
    unsigned long len; //記錄連結串列的長度,獲取長度操作可以O(1)返回
} list;

下面是插入了“hello“、”world“、”redis“ 三個元素的示意圖:
在這裡插入圖片描述

2、建立

listCreate 建立一個空元素的連結串列, 結構體中元素都賦0值。

/* 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)
        return NULL;
    list->head = list->tail = NULL;
    list->len = 0;
    list->dup = NULL;
    list->free = NULL;
    list->match = NULL;
    return list;
}

3、插入節點

redis雙向連結串列插入實現了三種插入方法,分別是頭插入,尾插入,特定節點前後插入。下面分別來看下三種方法:

3.1 連結串列頭插入

/* Add a new node to the list, to head, containing the specified 'value'
 * pointer as value.
 *
 * On error, NULL is returned and no operation is performed (i.e. the
 * list remains unaltered).
 * On success the 'list' pointer you pass to the function is returned. */
list *listAddNodeHead(list *list, void *value)
{
    listNode *node;

    if ((node = zmalloc(sizeof(*node))) == NULL) //先申請一個新的node節點大小的記憶體給新node節點使用
        return NULL;
    node->value = value;
    if (list->len == 0) { //連結串列為空的情況
        list->head = list->tail = node; //step 1 對應下圖中的圈1
        node->prev = node->next = NULL; //step 2 對應下圖中的圈2
    } else {//連結串列不為空
        node->prev = NULL; //step1
        node->next = list->head; //step2
        list->head->prev = node; //step3
        list->head = node; //step4
    }
    list->len++;//更新連結串列長度
    return list;
}

下面是連結串列頭插入兩種情況的圖示:

  • 1、連結串列為空的情況

在這裡插入圖片描述

  • 2、連結串列不為空的情況

在這裡插入圖片描述

3.2 連結串列尾插入

在連結串列尾部插入和在連結串列頭部插入一樣,也是分為兩種情況:1、連結串列為空的情況, 2、連結串列不為空的情況。其中連結串列為空的情況時和在連結串列頭插入一樣。

list *listAddNodeTail(list *list, void *value)
{
    listNode *node;

    if ((node = zmalloc(sizeof(*node))) == NULL)//先申請一個新的node節點大小的記憶體給新node節點使用
        return NULL;
    node->value = value;
    if (list->len == 0) {
        list->head = list->tail = node;//和連結串列頭插入邏輯一樣
        node->prev = node->next = NULL;
    } else {
        node->prev = list->tail;//step1
        node->next = NULL; //step2
        list->tail->next = node;//step3
        list->tail = node;//step4
    }
    list->len++; //更新連結串列長度
    return list;
}

連結串列為空的示意圖參考在頭部插入的示意圖,下圖為連結串列不為空時在尾部插入的示意圖:

在這裡插入圖片描述

3.3 連結串列某節點前後插入

list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
    listNode *node;

    if ((node = zmalloc(sizeof(*node))) == NULL) //先申請一個新的node節點大小的記憶體給新node節點使用
        return NULL;
    node->value = value;
    if (after) {//在指定節點後面插入
        node->prev = old_node; //讓新節點prev指向舊節點
        node->next = old_node->next;//讓新節點的next指向舊節點的next
        if (list->tail == old_node) { //有可能是在尾節點後插入新的節點,退化成了尾節點後插入的情況,示意圖見上圖3
            list->tail = node;
        }
    } else {//預設在指定節點前面插入
        node->next = old_node;//step1
        node->prev = old_node->prev; //step2
        if (list->head == old_node) {//有可能是在首節點前插入新節點, 退化成了頭節點前面插入的情況,示意圖建上圖2
            list->head = node;//step3
        }
    }

    if (node->prev != NULL) {
        //在指定節點後面插入時,更新舊節點的next指向新節點
        node->prev->next = node;
    }
    if (node->next != NULL) {
        //在指定節點前面插入時,更新舊節點的pre指向新節點
        node->next->prev = node;//step4
    }
    list->len++;//更新長度
    return list;
}

示意圖如下,圈中的數字對應程式碼中的step
在這裡插入圖片描述

4、刪除節點

/* 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不是第一個節點
        node->prev->next = node->next;
    else
        list->head = node->next; //是第一個節點的情況
    if (node->next) //說明node不是最後一個節點
        node->next->prev = node->prev;
    else
        list->tail = node->prev;//是最後一個節點的情況
    if (list->free) list->free(node->value);
    zfree(node);//釋放記憶體
    list->len--;//更新長度
}

5、合併連結串列

/* 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;

    if (l->tail)
        l->tail->next = o->head;
    else
        l->head = o->head;

    if (o->tail) l->tail = o->tail;
    l->len += o->len;

    /* Setup other as an empty list. */
    o->head = o->tail = NULL;
    o->len = 0;
}

6、總結

看程式碼終究不如自己實現一遍,練習網站奉上,大家可以去這裡練習一下,自己實現一遍。

https://leetcode-cn.com/problems/design-linked-list/