redis 的鍊錶簡單解析

2021-09-30 14:00:26 字數 971 閱讀 7369

鍊錶作為電腦程式最常用的資料結構之一,redis當然也提供了實現。redis 在 adlist檔案中實現了雙端鍊錶。

掌握鍊錶的核心思想,不同語言的鍊錶實現基本上都一樣。在redis中,首先用 乙個結構體 listnode 定義了鍊錶的每個節點

typedef

struct listnode listnode;

而鍊錶本身的實現結構如下

typedef

struct

list listiter;

/* returns a list iterator 'iter'. after the initialization every

* call to listnext() will return the next element of the list.

* * this function can't fail. */

listiter *listgetiterator(list

*list, int direction)

listnode *listnext(listiter *iter)

return current;

}

鍊錶的操作 還包括 建立、增加、刪除、等,基本上 redis中煉表的**實現是非常簡潔易懂的。\

不知道大家有沒有注意到listnodevalue的型別是萬能指標void*,所以該鍊錶是多型的,針對不同型別值的節點的操作,可以通過list結構的 dup、free、match 三個屬性點為 節點值設定型別特定的操作函式。

typedef

struct listnode listnode;

redis原始碼解析之鍊錶結構

typedef struct listnode listnode 雙端鍊錶節點包含2個指標域和1個資料域,注意資料的型別為void 因此其可以承載任意資料型別。typedef struct list list 雙端鍊錶中,使用函式指標來封裝與節點值相關的操作,在後面的使用中較頻繁,並維護乙個len作...

Redis 鍊錶

定義 每個鍊錶節點使用乙個 adlist.h listnode 結構來表示 typedef struct listnode listnode adlist.h list 列表結構 typedef struct list list 特性 redis 的鍊錶實現的特性可以總結如下 雙端 鍊錶節點帶有 p...

Redis 鍊錶和鍊錶節點

每個鍊錶節點使用乙個 adlist.h listnode 結構來表示 typedef struct listnode listnode 多個 listnode 可以通過 prev 和 next 指標組成雙向鍊錶。使用 adlist.h list 來持有鍊錶,操作起來會更方便 typedef stru...