/* 向list中的链表头部添加值为value的结点 */ /* Add a new node to the list, to head, contaning 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) { listNode *node;
/* 在链表的尾部添加值为value的结点 */ /* Add a new node to the list, to tail, contaning 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) { listNode *node;
/* 从链表中删除指定的结点 */ /* 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. */ voidlistDelNode(list *list, listNode *node) { if (node->prev) /* 删除的不是头结点 */ node->prev->next = node->next; else/* 删除的是头结点 */ list->head = node->next; if (node->next) /* 删除的不是尾结点 */ node->next->prev = node->prev; else/* 删除的是尾结点 */ list->tail = node->prev; if (list->free) list->free(node->value); /* 如果指定了free函数,*/ /*对当前结点的value进行free操作 */ zfree(node); /* 释放掉整个结点 */ list->len--; /* 链表长度减1 */ }
从链表中删除指定的结点,如果指定了free函数,需要对value进行free操作。
6. listGetIterator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 创建指定方向的迭代器 */ /* 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) { listIter *iter;
/* Release the iterator memory */ voidlistReleaseIterator(listIter *iter){ zfree(iter); }
释放迭代器。
8. listRewind
1 2 3 4 5
/* Create an iterator in the list private iterator structure */ voidlistRewind(list *list){ list->iter.next = list->head; list->iter.direction = AL_START_HEAD; }
/* 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 = listGetItarotr(list,<direction>); * while ((node = listNextIterator(iter)) != NULL) { * DoSomethingWith(listNodeValue(node)); * } * * */ listNode *listNext(listIter *iter) { listNode *current = iter->next;
/* 复制list */ /* 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) { list *copy; listIter *iter; listNode *node;
/* 返回等于指定key的结点 */ /* 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) { listIter *iter; listNode *node;
/* 返回指定索引的结点 */ /* 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 penultimante * and so on. If the index is out of range NULL is returned. */ listNode *listIndex(list *list, int 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; }