Redis源碼-資料結構之Adlist雙端鏈表

來源:互聯網
上載者:User

標籤:開源項目   資料結構   源碼   

Redis的Adlist實現了資料結構中的雙端鏈表,整個結構如下:
鏈表節點定義:
typedef struct listNode {    struct listNode *prev;    struct listNode *next;    void *value;} listNode;
鏈表定義:
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;
其中的三個函數指標先不用管,後面遇到了再看具體是幹什麼的,另外還實現了一個迭代器,有點c++的味道在裡面
typedef struct listIter {    listNode *next;    int direction;} listIter;

鏈表三要素,建立,插入,和刪除
list *listCreate(void){    struct list *list;    if ((list = malloc(sizeof(*list))) == NULL)        return NULL;    list->head = list->tail = NULL;    list->len = 0;    list->dup = NULL;    list->free = NULL;    list->match = NULL;    return list;}

插入分為從頭部插入和尾部插入,原始碼實現頭部都有非常清晰的注釋,告訴這個函數的一些細節,作者很是用心:
list *listAddNodeHead(list *list, void *value){    listNode *node;    if ((node = malloc(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 = NULL;        node->next = list->head;        list->head->prev = node;        list->head = node;    }    list->len++;    return list;}

釋放記憶體
void listRelease(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(current);        current = next;    }    free(list);}

迭代器的建立,以後可以效仿這種做法,迭代器分方向:
/* 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){    listIter *iter;    if ((iter = malloc(sizeof(*iter))) == NULL) return NULL;    if (direction == AL_START_HEAD)        iter->next = list->head;    else        iter->next = list->tail;    iter->direction = direction;    return iter;}

Redis源碼-資料結構之Adlist雙端鏈表

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.