@LIUHUAN
2019-03-11T14:32:49.000000Z
字数 8068
阅读 1138
redis
// 链表节点typedef struct listNode {struct listNode *prev; // 前驱struct listNode *next; // 后继void *value; // 值} listNode;// 链表迭代器typedef struct listIter {listNode *next; // 下一个节点int direction; // 迭代方向} listIter;// 迭代方向#define AL_START_HEAD 0#define AL_START_TAIL 1// 链表定义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;
/* Functions implemented as macros */#define listLength(l) ((l)->len) // 获取链表长度#define listFirst(l) ((l)->head) // 获取链表头部节点#define listLast(l) ((l)->tail) // 获取链表尾部节点#define listPrevNode(n) ((n)->prev) // 获取某个节点的前驱节点#define listNextNode(n) ((n)->next) // 获取某个节点的后继节点#define listNodeValue(n) ((n)->value) // 获取某个节点的值#define listSetDupMethod(l,m) ((l)->dup = (m)) // 设置节点复制函数#define listSetFreeMethod(l,m) ((l)->free = (m)) // 设置节点释放函数#define listSetMatchMethod(l,m) ((l)->match = (m))// 设置节点匹配函数#define listGetDupMethod(l) ((l)->dup) // 获取节点复制函数#define listGetFree(l) ((l)->free) // 获取节点释放函数#define listGetMatchMethod(l) ((l)->match) // 获取节点匹配函数
/* Prototypes */list *listCreate(void);void listRelease(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);void listDelNode(list *list, listNode *node);listIter *listGetIterator(list *list, int direction);listNode *listNext(listIter *iter);void listReleaseIterator(listIter *iter);list *listDup(list *orig);listNode *listSearchKey(list *list, void *key);listNode *listIndex(list *list, long index);void listRewind(list *list, listIter *li);void listRewindTail(list *list, listIter *li);void listRotate(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)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); // 释放整个链表管理节点}/* 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)return NULL;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)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) {// 节点之后插入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. */// 删除某个节点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--; // 链表长度减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) 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;}/* 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)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;}/* 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;}/* 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. */// 旋转链表,把尾部节点删除,插入到头部void listRotate(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;}