); * 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的链表结构】
推荐阅读
-
海峡快讯|美以警告莫站错队,伊扣押印度油轮亮明态度,伊朗头号油田被炸
-
中国新闻网|隔离14天参展值不值?香港企业“用脚投票”
-
-
-
LOL嘴强王者|天克脆皮与刺客,却为何一直火不起来,王者峡谷“五五开”
-
-
-
-
印度|国产手机在印度热度依然不减!销量前五占据四席
-
TA潮报|宋妍霏还是会穿,格子毛衣搭配休闲牛仔裤,身材还是那么高挑
-
孕期高危因素关乎性命!她们在帮助孕妇顺利跨过“生门”
-
时尚街拍酱搭|黄色束脚鞋让脚型更完美,纯白色连衣裙穿出小仙女的既视感
-
「红星新闻科技圈」大厂降维打击小厂机会渺茫,一季度国内5G套餐用户新增近三千万
-
「新京报」瑞幸咖啡被查?总部门店正常营业,员工正常上班
-
央企|陕视对话央企 | 哈尔滨电气集团有限公司党委书记、董事长——斯泽夫
-
-
融媒速看|发生侧翻致夫妻失踪1名消防员牺牲,3名消防员用皮艇救被困夫妻
-
-
-