Redis源码学习之【链表】

来源:互联网 发布:手机端网页小游戏源码 编辑:程序博客网 时间:2024/05/29 04:45

介绍

Redis自己实现了一个含有迭代器的双向链表。基本功能就是通用的双向链表,源码实现还是值得阅读以下的。

源文件

adlist.h adlist.c

分析

这里主要介绍其主要的数据结构其他的链表相关的操作有兴趣的话可以自己去看源码,其中的细节tricky还是挺多的。

[cpp] view plaincopy
  1. /* Node, List, and Iterator are the only data structures used currently. */  
  2.   
  3. /* 
  4.  * 链表节点 
  5.  */  
  6. typedef struct listNode {  
  7.   
  8.     // 前驱节点  
  9.     struct listNode *prev;  
  10.   
  11.     // 后继节点  
  12.     struct listNode *next;  
  13.   
  14.     // 值  
  15.     void *value;  
  16.   
  17. } listNode;  
  18.   
  19. /* 
  20.  * 链表迭代器 
  21.  */  
  22. typedef struct listIter {  
  23.   
  24.     // 下一节点  
  25.     listNode *next;  
  26.   
  27.     // 迭代方向  
  28.     int direction;  
  29.   
  30. } listIter;  
  31.   
  32. /* 
  33.  * 链表 
  34.  */  
  35. typedef struct list {  
  36.   
  37.     // 表头指针  
  38.     listNode *head;  
  39.   
  40.     // 表尾指针  
  41.     listNode *tail;  
  42.   
  43.     // 节点数量  
  44.     unsigned long len;  
  45.   
  46.     // 复制函数  
  47.     void *(*dup)(void *ptr);  
  48.     // 释放函数  
  49.     void (*free)(void *ptr);  
  50.     // 比对函数  
  51.     int (*match)(void *ptr, void *key);  
  52. } list; 
0 0
原创粉丝点击