源码adlist.c adlist.h,先来看看adlist的结构
/* Node, List, and Iterator are the only data structures used currently. */ typedef struct listNode { struct listNode *prev; struct listNode *next; void *value; } listNode; //标准的双向链表 typedef struct listIter { listNode *next; int direction; } listIter; //迭代器 typedef struct list { listNode *head; listNode *tail; void *(*dup)(void *ptr); void (*free)(void *ptr); int (*match)(void *ptr, void *key); unsigned long len; } 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) return NULL; 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. */ void listRelease(list *list) { unsigned long 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); } //一个创建,一个释放,很干净的代码 //释放时取链表的头部指针,while循环,头部依次next,一个一个free
/* 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; if ((node = zmalloc(sizeof(*node))) == NULL) return NULL; node->value = value; if (list->len == 0) {//如果链表长度还是0,则说明这加入的节点是唯一的节点,则头尾都是这个节点 list->head = list->tail = node; node->prev = node->next = NULL; } else { //这是插入头部的,所以新插入的节点为新的head node->prev = NULL; node->next = list->head; list->head->prev = node; list->head = node; } list->len++;//记得把链表长度+1 return list; }
/* 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; if ((node = zmalloc(sizeof(*node))) == NULL) return NULL; 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) return NULL; node->value = value; if (after) { //这个after意思是插入到old_node的后面(这里我来打个比方,1,2,3,5,6,把4插入到3的后面,4的前节点赋值为3,4的后节点赋值为3的现在的后节点5) node->prev = old_node; node->next = old_node->next; if (list->tail == old_node) { list->tail = node; } } else { //4的后节点赋值为3,4的前节点赋值为3的现在的前节点2 node->next = old_node; node->prev = old_node->prev; if (list->head == old_node) { list->head = node; } } if (node->prev != NULL) { //先看after,4的前节点3的后节点赋值为4,不赋值前3的后节点是5 //再看非after,4的前节点2的后节点赋值为4,不赋值前2的后节点是3 node->prev->next = node; } if (node->next != NULL) { //先看after,4的后节点5的前节点赋值为4,不赋值前5的前节点是3,这样最后就变成了1,2,3,4,5,6 //再看非after,4的后节点3的前节点赋值为4,不赋值前3的后节点是5,这样最后就变成了1,2,4,3,5,6 node->next->prev = node; } list->len++;//记得长度+1 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. */ //把中间的删除,就是让删除的前节点的next指向删除的next,删除的后节点的prev指向删除的prev,再注意一下头尾特殊情况,长度-1 void listDelNode(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--; }
/* 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) return NULL; if (direction == AL_START_HEAD) iter->next = list->head; else iter->next = list->tail; iter->direction = direction; return iter; } /* Release the iterator memory */ void listReleaseIterator(listIter *iter) { zfree(iter); } /* Create an iterator in the list private iterator structure */ void listRewind(list *list, listIter *li) { li->next = list->head; li->direction = AL_START_HEAD; } void listRewindTail(list *list, listIter *li) { li->next = list->tail; li->direction = AL_START_TAIL; } //迭代器的创建与释放,迭代器是单向的,不是从head开始就是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 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) return NULL; 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); return NULL; } } else value = node->value; if (listAddNodeTail(copy, value) == NULL) { listRelease(copy); listReleaseIterator(iter); return NULL; } } listReleaseIterator(iter); return copy; } //复制整个list,并且释放old,利用迭代器,一个节点一个节点的复制并释放
/* 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); return NULL; } //查找,迭代器遍历,注意释放迭代器,listReleaseIterator出现了三次
/* 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, 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; } //index可以传负值,表示从尾部计算第几个,正值则是从头部计算第几个,0表示头部第一个,-1表示尾部第一个
/* 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; /* Detatch 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; } //这个方法是将链表的尾部变成链表的头部,即1,2,3,4变成4,1,2,3
总体来看代码比较简单,是个典型的双向链表,但代码清晰,没有多余的逻辑,以后可以做为它用。