redis源码分析(adlist)
2017-03-09 15:39
357 查看
typedef struct listNode { struct listNode *prev; struct listNode *next; void *value; } listNode;
首先定义了一个节点,包含前驱和后继以及对应的value
typedef struct listIter {
listNode *next;
int direction;
} listIter;
list的迭代器,next指针和迭代方向
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;链表内容 head和tail指针分别指向收尾,另外三个函数指针代表在拷贝,删除,对比的时候,如果有函数可调用就调用当前次函数的逻辑,不然就调用自身的逻辑
/* 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;
}
/* Free the whole list.
*
* This function can't fail. */
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);
zfree(current);
current = next;
}
zfree(list);
}
创建和删除链表没什么说的
/* 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)
return NULL;
node->value = value;
//长度为0的,增加一个那么头尾指针都指向这个节点
//先让node的前驱指向null,node的后继指向头节点,然后让头节点的前驱指向node,最后在把头节点指向新的Node节点
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; }在头和尾部ADD道理一致
list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
listNode *node;
if ((node = zmalloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
//在old_node前还是后insert if (after) { node->prev = old_node; node->next = old_node->next; if (list->tail == old_node) { list->tail = node; } } else { 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; }
相关文章推荐
- redis源码分析(9)redis源码链表学习总结 adlist.h adlist.c
- 【redis源码分析】双向链表---adlist
- redis源码分析之数据结构(一)链表adlist.c
- Redis源码分析(adlist)
- Redis源码分析(二)——链表adlist
- Redis源码阅读笔记—adlist
- redis源码分析(四)、redis命令学习总结—链表List
- redis源码系列-数据结构(adlist/ziplist/dict)
- 源码分析redis的有序集合,学习skiplist跳跃表数据结构
- redis源码系列-数据结构(adlist/ziplist/dict)
- redis源码分析(3)-- 基本数据结构双链表list
- Redis源码分析(skiplist)
- Redis源码分析(九)--- t_list,t_string的分析
- redis源码分析(八)、redis数据结构之压缩ziplist--------ziplist.c ziplist.h学习笔记
- redis源码解读之双向链表————adlist.h文件
- Redis skip list结构分析
- redis dict.c源码分析
- Nginx 源码分析-- ngx_array、ngx_list基本数据结构
- xm list源码分析
- Redis源码解析4 - 数据类型之 String & List