麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 數據庫 > Redis > 正文

詳解Redis中的雙鏈表結構

2020-03-17 12:41:51
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Redis中的雙鏈表結構,包括listNode結構的API,需要的朋友可以參考下
 

Redis中雙鏈表實現的基本結構:
1.節點結構

typedef struct listNode {  struct listNode *prev; //前向節點  struct listNode *next; //后向節點  void *value;       //該節點的值} listNode;

2.雙向鏈表結構

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;

3.雙向鏈表遍歷器

typedef struct listIter {  listNode *next;  //下一個節點  int direction;} listIter; 方向定義  #define AL_START_HEAD 0 //向前查找  #define AL_START_TAIL 1  //向后查找

4.宏定義函數

#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))#define listSetFreeMethod(l,m) ((l)->free = (m))#define listSetMatchMethod(l,m) ((l)->match = (m))#define listGetDupMethod(l) ((l)->dup)#define listGetFree(l) ((l)->free)#define listGetMatchMethod(l) ((l)->match)

5.定義函數

list *listCreate(void); //創建一個新的鏈表。該鏈表可以使用AlFree()方法釋放。               //但使用AlFree()方法前需要釋放用戶釋放私有節點的值。               //如果沒有創建成功,返回null;創建成功則返回指向新鏈表的指針。void listRelease(list *list); //釋放整個鏈表,此函數不會執行失敗。調用zfree(list *list)方法,定義在Zmalloc.c中。list *listAddNodeHead(list *list, void *value); //向鏈表頭部中增加一個節點list *listAddNodeTail(list *list, void *value);  //向鏈表尾部增加一個節點list *listInsertNode(list *list, listNode *old_node, void *value, int after);//向某個節點位置插入節點 after為方向void listDelNode(list *list, listNode *node);//從鏈表上刪除特定節點,調用者釋放特定私用節點的值。                              //該函數不會執行失敗listIter *listGetIterator(list *list, int direction);//返回某個鏈表的迭代器。                                 //迭代器的listNext()方法會返回鏈表的下個節點。direction是方向                                //該函數不會執行失敗。listNode *listNext(listIter *iter);        void listReleaseIterator(listIter *iter);      //釋放迭代器的內存。list *listDup(list *orig);                //復制整個鏈表。當內存溢出時返回null,成功時返回原鏈表的一個備份                                //不管該方法是否執行成功,原鏈表不會改變。listNode *listSearchKey(list *list, void *key); //從特定的鏈表查找key。成功則返回第一個匹配節點的指針                                //如果沒有匹配,則返回null。listNode *listIndex(list *list, long index);   //序號從0開始,鏈表的頭的索引為0.1為頭節點的下個節點。一次類推。                            //負整數用來表示從尾部開始計數。-1表示最后一個節點,-2倒數第二個節點                             //如果超過鏈表的索引,則返回nullvoid listRewind(list *list, listIter *li) {  li->next = list->head;  li->direction = AL_START_HEAD;}void listRewindTail(list *list, listIter *li) {  li->next = list->tail;  li->direction = AL_START_TAIL;}void listRotate(list *list);         //旋轉鏈表,移除尾節點并插入頭部。

 

list結構和listNode結構的API
list和listNode都有它們自己的一族API,這里貼出來學習一下redis的源碼(ps:下面的代碼都是我仿照redis改寫能直接編譯運行的代碼)

list *listCreate(void)

  /**    * 創建一個新列表    *    * T = O(1)                                                                  */   list *listCreate(void)   {     struct list *list;        // 為列表結構分配內存     list = (struct list *)malloc(sizeof(struct list));     if (list == NULL)       return NULL;        // 初始化屬性     list->head = list->tail = NULL;     list->len = 0;     list->dup = NULL;     list->free = NULL;     list->match = NULL;        return list;   } 


void listRelease(list *list)

 

  /**    * 釋放整個列表    *    * T = O(N), N為列表長度    */   void listRelease(list *list)   {     unsigned long len;     listNode *current, *next;        current = list->head;     len = list->len;        while (len --) {       next = current->next;       // 如果列表有自帶的free方法,那么先對節點值調用它       if (list->free) list->free(current->value);       // 之后釋放節點       free(current);       current = next;     }     free(list);   }  

list *listAddNodeHead(list *list, void *value)
  /**    * 新建一個包含給定value的節點,并將它加入到列表的表頭    *    * T = O(1)                                                                  */   list *listAddNodeHead(list *list, void *value)   {     listNode *node;        node = (listNode *)malloc(sizeof(listNode));     if (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;   } 


list *listAddNodeTail(list *list, void *value)

  /**    * 新建一個包含給定value的節點,并把它加入到列表的表尾    *    * T = O(1)    */   list *listAddNodeTail(list *list, void *value)   {     listNode *node;          node = (listNode *)malloc(sizeof(listNode));     if (node == NULL)       return NULL;        if (list->len == 0) {       // 第一個節點       list->head = list->tail = node;       node->prev = node->next = NULL;     } else {       // 不是第一節點       node->prev = list->tail;       node->next = NULL;       list->tail->next = node;       list->tail = node;     }        list->len ++;        return list;   } 


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

 

  /**    * 創建一個包含值value的節點    * 并根據after參數的指示,將新節點插入到old_node的之前或者之后    *    * T = O(1)    */   list *listInsertNode(list *list, listNode *old_node, void *value, int after)   {     listNode *node;        node = (listNode *)malloc(sizeof(listNode));     if (node == NULL)       return NULL;        if (after) {       // 插入到old_node之后       node->prev = old_node;       node->next = old_node->next;       // 處理表尾節點       if (list->tail == old_node) {         list->tail = node;       }     } else {       // 插入到old_node之前       node->next = old_node;       node->prev = old_node->prev;       // 處理表頭節點       if (list->head == old_node) {         list->head = node;       }     }        // 更新前置節點和后繼節點的指針(這個地方很經典,節約代碼)     if (node->prev != NULL) {       node->prev->next = node;     }     if (node->next != NULL) {       node->next->prev = node;     }        // 更新列表節點     list->len ++;        return list;   } 


void listDelNode(list *list, listNode *node)

  

 /**    * 釋放列表中給定的節點    *    * T = O(1)    */   void listDelNode(list *list, listNode *node)   {     // 處理前驅節點指針     if (node->prev) {       node->prev->next = node->next;     } else {       list->head = node->next;     }        // 處理后繼節點     if (node->next) {       node->next->prev = node->prev;     } else {       list->tail = node->prev;     }        // 釋放節點值     if (list->free) list->free(node->value);        // 釋放節點     free(node);        // 更新列表節點數目     list->len --;   } 


迭代器
其實我對迭代器的概念非常陌生,因為我是純c程序員,不會c++,這里直接跟著學了!

Redis針對list結構實現了一個迭代器,用于對鏈表進行遍歷

迭代器的結構定義如下:

  /**    * 鏈表迭代器    */   typedef struct listIter {     // 下一節點     listNode *next;        // 迭代方向     int direction;   } listIter; 


direction決定了迭代器是沿著next指針向后迭代,還是沿著prev指針向前迭代,這個值可以是adlist.h中的AL_START_HEAD常量或AL_START_TAIL常量:

  #define AL_START_HEAD 0   #define AL_START_TAIL 1 


學習一下迭代器的api實現:

listIter *listGetIterator(list *list, int direction)

  /**    * 創建列表list的一個迭代器,迭代方向由參數direction決定    *    * 每次對迭代器listNext(),迭代器返回列表的下一個節點    *    * T = O(1)    */   listIter *listGetIterator(list *list, int direction)   {     listIter *iter;        iter = (listIter *)malloc(sizeof(listIter));     if (iter == NULL)       return NULL;        // 根據迭代器的方向,將迭代器的指針指向表頭或者表尾     if (direction == AL_START_HEAD) {       iter->next = list->head;     } else {       iter->next = list->tail;     }        // 記錄方向     iter->direction = direction;        return iter;   } 


void listRewind(list *list, listIter *li)

  /**    * 將迭代器iter的迭代指針倒回list的表頭    *    * T = O(1)    */   void listRewind(list *list, listIter *li)   {     li->next = list->head;     li->direction = AL_START_HEAD;   } 


void listRewindTail(list *list, listIter *li)

  /**    * 將迭代器iter的迭代指針倒回list的表尾    *    * T = O(1)    */   void listRewindTail(list *list, listIter *li)   {     li->next = list->tail;     li->direction = AL_START_TAIL;   } 


listNode *listNext(listIter *iter)

  /**    * 函數要么返回當前節點,要么返回NULL,因此,常見的用法是:    * iter = listGetIterator(list, <direction>);    * while ((node = listNext(iter)) != NULL) {    *   doSomethingWith(listNodeValue(node));    * }    *    * T = O(1)    */   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;   } 
 


注:相關教程知識閱讀請移步到Redis頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产chinesehd精品91 | 国产精品欧美日韩一区二区 | 黄色高清免费网站 | 香蕉国产片 | 免费在线观看毛片视频 | 91在线播放国产 | 欧美黄 片免费观看 | 爱高潮www亚洲精品 欧美黄色一级片视频 | 十级毛片 | 日韩欧美激情视频 | 国产精品久久久久久久av | 久久tv免费国产高清 | 在线成人免费观看视频 | 久草在线高清 | 亚洲成人精品在线 | 黑人一区二区三区四区五区 | 精品一区二区在线观看 | 一级黄色大片在线观看 | 成人区精品一区二区婷婷 | 国产羞羞视频在线观看 | 主播粉嫩国产在线精品 | 国产精品二区高清在线 | 成年人小视频在线观看 | 一级电影免费在线观看 | 素人视频在线观看免费 | 久久久在线免费观看 | chengrenyingshi| 免费黄色在线电影 | 黄色免费小视频网站 | 色就色 综合偷拍区91网 | 一级在线观看视频 | 久久久久久久久久久国产精品 | 曰批全过程40分钟免费视频多人 | 爱唯侦察 国产合集 亚洲 | 一级尻逼视频 | 一级在线视频 | 天天草天天干天天射 | 色999国产 | 中文字幕h | 欧美国产综合视频 | 77成人影院 |