); * while ((node = listNext(iter)) != NULL) { * doSomethingWith(listNodeValue(node)); * } * * */listNode *listNext(listIter *iter) //返回迭代器iter指向的当前节点并更新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; //返回备份的当前节点地址}/* Duplicate the whole list. On out of memory NULL is returned. * On success a copy of the original list is returned. * * The 'Dup' method set with listSetDupMethod() function is used * to copy the node value. Otherwise the same pointer value of * the original node is used as value of the copied node. * * The original list both on success or error is never modified. */list *listDup(list *orig) //拷贝表头为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定义一个迭代器并设置迭代方向 while((node = listNext(iter)) != NULL) { //迭代器根据迭代方向不停迭代 void *value; //复制节点值到新节点 if (copy->dup) { value = copy->dup(node->value); //如果定义了list结构中的dup指针,则使用该方法拷贝节点值 。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; //返回拷贝副本}/* Search the list for a node matching a given key. * The match is performed using the 'match' method * set with listSetMatchMethod(). If no 'match' method * is set, the 'value' pointer of every node is directly * compared with the 'key' pointer. * * On success the first matching node pointer is returned * (search starts from head). If no matching node exists * NULL is returned. */listNode *listSearchKey(list *list, void *key) //在list中查找value为key的节点并返回{ listIter *iter; listNode *node; iter = listGetIterator(list, AL_START_HEAD); //创建迭代器 while((node = listNext(iter)) != NULL) { //迭代整个链表 if (list->match) { //如果设置list结构中的match方法,则用该方法比较 if (list->match(node->value, key)) { listReleaseIterator(iter); //如果找到,释放迭代器返回node地址 return node; } } else { if (key == node->value) { listReleaseIterator(iter); return node; } } } listReleaseIterator(iter); //释放迭代器 return NULL;}/* Return the element at the specified zero-based index * where 0 is the head, 1 is the element next to head * and so on. Negative integers are used in order to count * from the tail, -1 is the last element, -2 the penultimate * and so on. If the index is out of range NULL is returned. */listNode *listIndex(list *list, long index) { //返回下标为index的节点地址 listNode *n; if (index < 0) { index = (-index)-1; //如果下标为负数,从链表尾部开始 n = list->tail; while(index-- && n) n = n->prev; } else { n = list->head; //如果下标为正数,从链表头部开始 while(index-- && n) n = n->next; } return n;}/* Rotate the list removing the tail node and inserting it to the head. */void listRotate(list *list) { //将尾节点插到头结点 listNode *tail = list->tail; if (listLength(list) <= 1) return; //只有一个节点或空链表直接返回 /* Detach current tail */ list->tail = tail->prev; //取出尾节点,更新list的tail指针 list->tail->next = NULL; /* Move it as head */ list->head->prev = tail; //将节点插到表头,更新list的head指针 tail->prev = NULL; tail->next = list->head; list->head = tail;}
【Redis的链表结构】
推荐阅读
-
悠游侠|《宝可梦剑盾》钢属性和岩石属性宝可梦限时大量出现活动
-
【三月18城二手房成交量恢复至去年同期六成】机构:三月18城二手房成交量恢复至去年同期六成
-
贾斯汀·比伯|海莉·比伯和贾斯汀及朋友共进晚餐,白衬衫配牛仔裤秀性感小腹
-
台湾|南海紧张?台媒:官员证实台湾军方近期确实有不寻常举动
-
特朗普|特朗普真的要耍赖,扬言输了大选也不会走人!拜登:将派军队赶人
-
-
日本驻|文在寅欲起诉日本核污水入海,韩国民众拉黑日本海鲜
-
何超莲龙凤胎风波后首现身,素颜气色差显男相,像没发育好的学生
-
-
阿徐说|两人身份差距那么大,为什么娄昭君看上了高欢?,重视门第的时代
-
新华网■世卫组织:武汉新增病例为零 给世界带来希望
-
袁载誉X|到底可以有多“甜”?这有几个真实例子,青梅竹马
-
-
-
郭晶晶|“夫妻本是同林鸟”,但抱歉,霍启刚这一次也救不了郭晶晶
-
-
-
信贷|穆迪:中国影子银行资产两年半以来出现首次增长
-
2021大一新生军训是否取消-2021年大一新生还有军训吗-2021年大一新生军训时间
-