); * 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的链表结构】
推荐阅读
-
处女座|说分手就分手,不会优柔寡断的三个星座,天蝎座果断决绝
-
-
黄晓明 |许飞被批情商低?好心为黄晓明宣传,却在婚纱照baby的位置p上自己
-
-
青年|20张罕见的图片为您打开了您从未见过的世界的新视角
-
「善郡财经」畅谈职业生涯的智慧与勇气,卡奥斯COO严鹏飞走进中企视讯直播间
-
-
云朵上的兔子|长安UNI-T半月销量2万台,颜值抢眼,或成下一代国产SUV“领导者”
-
-
互联网科技秀中国芯更可靠 稳定压倒一切 跑分实测光威SSD弈系列Pro
-
-
「FX678」美油史上首次跌至负值,需求枯竭叠加储油能力告急
-
贾静雯:绿军核心也疯了!为了2.18亿超级顶薪,他甘愿和欧文一起叛变
-
合肥大维装饰|美式田园风新家装修,阳台+小吧台+客厅的设计,看得出主人公的品位
-
趣闻晨报■如何看待郭涛对此前〈父亲的力量〉一书中不当的观点道歉?
-
-
-
林夕探长|港口货物“堆积如山”,俄罗斯夺得机会,加拿大痛失中国市场
-
【牛录谈】直9将退居二线,中国海鹰来了—直20反潜型直升机亮相
-
清朝|雍正王朝:三阿哥也很优秀,为什么不被康熙看中呢?