); * 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的链表结构】
推荐阅读
-
汉服@超适合夏天的小清新汉服,日常又有活力,夏天也能清爽出门
-
搞笑刺猬|要不要撩,在线等!,趣图段子:妹子就在车后坐着
-
怀旧服|魔兽世界:怀旧服P6阶段,纳克萨玛斯副本,布甲皮甲冰抗装备
-
移动支付|“移动支付”真的有用嘛?英国教授一针见血,移动支付害惨了欧洲
-
南海网|纪念海南解放70周年 | 李振亚:血染红棉映丹心
-
大学生|“三不娶”行业引发网友争议,银行女也在其中,从业者连连叫屈
-
-
快乐小龙儿|显身材白皙,看的我心慌慌!,清爽苗条的长腿美女
-
-
-
足球情报君|丰塞卡也想留下他,状态出色受追捧!罗马当家前锋被国米尤文追逐
-
次元快讯|当理发师撩起她的刘海后,网友不淡定了,刘思瑶去理发店做造型
-
-
长城汽车|额定功率200kW、使用寿命超1.5万小时:长城发布大功率燃料电池发动机
-
问董秘|“轻量化汽车底盘数...,投资者提问:请介绍一下公司在智能制造方面的项目推进情况
-
【易车网】VS 观致5,18万元还在纠结什么?柯珞克
-
-
逆周期调控|太平洋证券:市场仍处于震荡蓄力 破局曙光渐行渐近
-
-
张兰|富婆张兰,终于在直播间说出了汪小菲离婚的真正原因!