/* Prototypes */
list *listCreate(void);voidlistRelease(list *list);
list *listAddNodeHead(list *list,void*value);
list *listAddNodeTail(list *list,void*value);
list *listInsertNode(list *list, listNode *old_node,void*value,int after);voidlistDelNode(list *list, listNode *node);
listIter *listGetIterator(list *list,int direction);
listNode *listNext(listIter *iter);voidlistReleaseIterator(listIter *iter);
list *listDup(list *orig);
listNode *listSearchKey(list *list,void*key);
listNode *listIndex(list *list,long index);voidlistRewind(list *list, listIter *li);voidlistRewindTail(list *list, listIter *li);voidlistRotate(list *list);
具体实现
/* Create a new list. The created list can be freed with
* AlFreeList(), but private value of every node need to be freed
* by the user before to call AlFreeList().
*
* On error, NULL is returned. Otherwise the pointer to the new list. */// 创建链表
list *listCreate(void){struct list *list;// 分配内存if((list =zmalloc(sizeof(*list)))==NULL)returnNULL;// 初始化
list->head = list->tail =NULL;
list->len =0;
list->dup =NULL;
list->free =NULL;
list->match =NULL;return list;}/* Free the whole list.
*
* This function can't fail. */// 释放链表voidlistRelease(list *list){unsignedlong len;
listNode *current,*next;
current = list->head;
len = list->len;// 链表长度while(len--){
next = current->next;if(list->free) list->free(current->value);// 如果有值的释放函数调用zfree(current);// 释放内存
current = next;}zfree(list);// 释放整个链表管理节点}/* Add a new node to the list, to head, containing 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;// 为节点分配内存if((node =zmalloc(sizeof(*node)))==NULL)returnNULL;
node->value = value;if(list->len ==0){// 添加前链表为空
list->head = list->tail = node;
node->prev = node->next =NULL;}else{// 已经存在头节点
node->prev =NULL;
node->next = list->head;
list->head->prev = node;
list->head = node;}
list->len++;// 增加长度return list;}/* Add a new node to the list, to tail, containing 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;if((node =zmalloc(sizeof(*node)))==NULL)returnNULL;
node->value = value;if(list->len ==0){// 链表为空
list->head = list->tail = node;
node->prev = node->next =NULL;}else{// 链表非空
node->prev = list->tail;
node->next =NULL;
list->tail->next = node;
list->tail = node;}
list->len++;// 增加长度return list;}// 在某个节点前(后)插入节点
list *listInsertNode(list *list, listNode *old_node,void*value,int after){
listNode *node;// 创建插入节点if((node =zmalloc(sizeof(*node)))==NULL)returnNULL;
node->value = value;if(after){// 节点之后插入
node->prev = old_node;
node->next = old_node->next;if(list->tail == old_node){// 在尾部节点之后插入节点
list->tail = node;}}else{// 节点之前插入
node->next = old_node;
node->prev = old_node->prev;if(list->head == old_node){// 在头部部节点之前插入节点
list->head = node;}}if(node->prev !=NULL){// 插入节点后,非头节点
node->prev->next = node;}if(node->next !=NULL){// 插入节点后,非尾部节点
node->next->prev = node;}
list->len++;return list;}/* 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);// 释放值函数zfree(node);
list->len--;// 链表长度减1}/* 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;if((iter =zmalloc(sizeof(*iter)))==NULL)returnNULL;if(direction == AL_START_HEAD)// 头部开始的迭代器
iter->next = list->head;else// 尾部开始的迭代器
iter->next = list->tail;
iter->direction = direction;// 迭代器方向return iter;}/* Release the iterator memory */// 释放迭代器内存voidlistReleaseIterator(listIter *iter){zfree(iter);}/* Create an iterator in the list private iterator structure */// 关联迭代器和链表,从头部开始迭代voidlistRewind(list *list, listIter *li){
li->next = list->head;
li->direction = AL_START_HEAD;}// 关联迭代器和链表,从尾部开始迭代voidlistRewindTail(list *list, listIter *li){
li->next = list->tail;
li->direction = AL_START_TAIL;}/* 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 = listGetIterator(list,<direction>);
* while ((node = listNext(iter)) != NULL) {
* doSomethingWith(listNodeValue(node));
* }
*
* */// 获取迭代器的下一个元素
listNode *listNext(listIter *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){
list *copy;
listIter *iter;
listNode *node;if((copy =listCreate())==NULL)returnNULL;// 函数复制
copy->dup = orig->dup;
copy->free = orig->free;
copy->match = orig->match;
iter =listGetIterator(orig, AL_START_HEAD);while((node =listNext(iter))!=NULL){void*value;if(copy->dup){
value = copy->dup(node->value);if(value ==NULL){// 复制节点值失败listRelease(copy);listReleaseIterator(iter);returnNULL;}}else// 没有复制函数,那么复制后的链表指向复制前的链表
value = node->value;if(listAddNodeTail(copy, value)==NULL){// 添加节点到尾部listRelease(copy);listReleaseIterator(iter);returnNULL;}}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){
listIter *iter;
listNode *node;
iter =listGetIterator(list, AL_START_HEAD);while((node =listNext(iter))!=NULL){if(list->match){// 值匹配函数if(list->match(node->value, key)){listReleaseIterator(iter);return node;}}else{// 直接使用==if(key == node->value){listReleaseIterator(iter);return node;}}}listReleaseIterator(iter);returnNULL;}/* 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){
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. */// 旋转链表,把尾部节点删除,插入到头部voidlistRotate(list *list){
listNode *tail = list->tail;if(listLength(list)<=1)return;/* Detach current tail */
list->tail = tail->prev;
list->tail->next =NULL;/* Move it as head */
list->head->prev = tail;
tail->prev =NULL;
tail->next = list->head;
list->head = tail;}