Redis的链表结构( 二 )


typedef struct listIter { listNode *next; //迭代器当前指向的节点(名字叫next有点迷惑) int direction; //迭代方向,可以取以下两个值:AL_START_HEAD和AL_START_TAIL} listIter#define AL_START_HEAD 0 //正向迭代:从表头向表尾进行迭代#define AL_START_TAIL 1 //反向迭代:从表尾到表头进行迭代在listDup函数中就使用了迭代器,listDup函数的定义如下:
//listDup的功能是拷贝一份链表list *listDup(list *orig){ list *copy; listIter *iter; listNode *node; if ((copy = listCreate()) == NULL) //创建一个表头 return NULL; //设置新建表头的处理函数 copy->dup = orig->dup; copy->free = orig->free; copy->match = orig->match; //迭代整个orig的链表,重点关注此部分 。iter = listGetIterator(orig, AL_START_HEAD);//为orig定义一个迭代器并设置迭代方向,在c++中例如是 vector<int>::interator it; while((node = listNext(iter)) != NULL) { //迭代器根据迭代方向不停迭代,相当于++it void *value; //复制节点值到新节点 if (copy->dup) { //如果定义了list结构中的dup指针,则使用该方法拷贝节点值 。value = https://www.isolves.com/it/sjk/Redis/2019-08-05/copy->dup(node->value); if (value == NULL) { listRelease(copy); listReleaseIterator(iter); return NULL; } } else value = node->value; //获得当前node的value值 if (listAddNodeTail(copy, value) == NULL) { //将node节点尾插到copy表头的链表中 listRelease(copy); listReleaseIterator(iter); return NULL; } } listReleaseIterator(iter); //自行释放迭代器 return copy; //返回拷贝副本迭代器的好处:

  • 提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示 。
  • 将指针操作进行了统一封装,代码可读性增强 。
3.3 adlist.c文件
刚才所有函数的定义如下:
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) //释放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); //如果设置了list结构的释放函数,则调用该函数释放节点值 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) //将value添加到list链表的头部{ listNode *node; if ((node = zmalloc(sizeof(*node))) == NULL) //为新节点分配空间 return NULL; node->value = https://www.isolves.com/it/sjk/Redis/2019-08-05/value; //设置node的value值 if (list->len == 0) { //将node头插到空链表 list->head = list->tail = node; node->prev = node->next = NULL; } else { //将node头插到非空链表 node->prev = NULL; node->next = list->head; list->head->prev = node; list->head = node; } list->len++; //链表元素计数器加1 return list;}/* Add a new node to the list, to tail, 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 *listAddNodeTail(list *list, void *value) //将value添加到list链表的尾部{ listNode *node; if ((node = zmalloc(sizeof(*node))) == NULL) //为新节点分配空间 return NULL; node->value = value; //设置node的value值 if (list->len == 0) { //将node尾插到空链表 list->head = list->tail = node; node->prev = node->next = NULL; } else { //将node头插到非空链表 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) //在list中,根据after在old_node节点前后插入值为value的节点 。{ listNode *node; if ((node = zmalloc(sizeof(*node))) == NULL) //为新节点分配空间 return NULL; node->value = value; //设置node的value值 if (after) { //after 非零,则将节点插入到old_node的后面 node->prev = old_node; node->next = old_node->next; if (list->tail == old_node) { //目标节点如果是链表的尾节点,更新list的tail指针 list->tail = node; } } else { //after 为零,则将节点插入到old_node的前面 node->next = old_node; node->prev = old_node->prev; if (list->head == old_node) { //如果节点如果是链表的头节点,更新list的head指针 list->head = node; } } if (node->prev != NULL) { //如果有,则更新node的前驱节点的指针 node->prev->next = node; } if (node->next != NULL) { //如果有,则更新node的后继节点的指针 node->next->prev = node; } list->len++; //更新链表节点计数器 return list;}/* Remove the specified node from the specified list. * It's up to the caller to free the private value of the node. * * This function can't fail. */void listDelNode(list *list, listNode *node) //从list删除node节点{ if (node->prev) //更新node的前驱节点的指针 node->prev->next = node->next; else list->head = node->next; if (node->next) //更新node的后继节点的指针 node->next->prev = node->prev; else list->tail = node->prev; if (list->free) list->free(node->value); //如果设置了list结构的释放函数,则调用该函数释放节点值 zfree(node); //释放节点 list->len--; //更新链表节点计数器}/* 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) //为list创建一个迭代器iterator{ listIter *iter; if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL; //为迭代器申请空间 if (direction == AL_START_HEAD) //设置迭代指针的起始位置 iter->next = list->head; else iter->next = list->tail; iter->direction = direction; //设置迭代方向 return iter;}/* Release the iterator memory */void listReleaseIterator(listIter *iter) { //释放iter迭代器 zfree(iter);}/* Create an iterator in the list private iterator structure */void listRewind(list *list, listIter *li) { //将迭代器li重置为list的头结点并且设置为正向迭代 li->next = list->head; //设置迭代指针的起始位置 li->direction = AL_START_HEAD; //设置迭代方向从头到尾}void listRewindTail(list *list, listIter *li) { //将迭代器li重置为list的尾结点并且设置为反向迭代 li->next = list->tail; //设置迭代指针的起始位置 li->direction = AL_START_TAIL; //设置迭代方向从尾到头}/* Return the next element of an iterator. * It's valid to remove the currently returned element using * listDelNode(), but not to remove other elements. * * The function returns a pointer to the next element of the list, * or NULL if there are no more elements, so the classical usage patter * is: * * iter = listGetIterator(list,


推荐阅读