//每个节点,有一个数据指针,一个指向之前和之后节点的指针
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;
list *listCreate(void)
{
struct list *list;
//申请一个struct list大小的内存
if ((list = zmalloc(sizeof(*list))) == NULL)
return NULL;
//相关成员都设置为NULL
list->head = list->tail = NULL;
list->len = 0;
list->dup = NULL;
list->free = NULL;
list->match = NULL;
return list;
}
void listEmpty(list *list)
{
unsigned long len;
listNode *current, *next;
current = list->head;
len = list->len;
//从头节点依次释放
while(len--) {
next = current->next;
//因为每个节点指向的value也是一个指针,因此需要先释放value
if (list->free) list->free(current->value);
//再释放当前的节点
zfree(current);
current = next;
}
list->head = list->tail = NULL;
list->len = 0;
}
除了移除所有节点之外,还把链表的相关成员都释放
void listRelease(list *list)
{
listEmpty(list);
zfree(list);
}
list *listAddNodeHead(list *list, void *value)
{
listNode *node;
//申请节点的内存
if ((node = zmalloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
//长度为0的话,头尾都是当前节点并且没有前后节点
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;
}
至于尾插法,这里不再赘述
list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
//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;
}
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--;
}
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;
}
这个函数比较有意思,返回的是迭代器指向的节点,然而函数内部迭代器指向了下一个节点
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;
}
所有的内存都是在堆中申请,因此复制起来也必须一个节点一个节点来申请内存
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;
listRewind(orig, &iter);
while((node = listNext(&iter)) != NULL) {
void *value;
//区分浅拷贝还是深拷贝,node内部的value实际上是一个指针
if (copy->dup) {
value = copy->dup(node->value);
if (value == NULL) {
listRelease(copy);//一旦复制过程中发生错误就释放已经申请的内存后返回NULL
return NULL;
}
} else {
value = node->value;
}
if (listAddNodeTail(copy, value) == NULL) {
/* Free value if dup succeed but listAddNodeTail failed. */
if (copy->free) copy->free(value);
listRelease(copy);
return NULL;
}
}
return copy;
}
这里就类似于list的遍历,找到符合要求的键
listNode *listSearchKey(list *list, void *key)
{
listIter iter;
listNode *node;
listRewind(list, &iter);
while((node = listNext(&iter)) != NULL) {
//如果定义了match函数,就调用match函数判断,不然就直接判断value指针的值一不一样
if (list->match) {
if (list->match(node->value, key)) {
return node;
}
} else {
if (key == node->value) {
return node;
}
}
}
return NULL;
}
至此,list的所有成员意义我们都已经知晓:
index为0是指的是head,-1时指的是尾部
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;
}
void listRotateTailToHead(list *list) {
if (listLength(list) <= 1) return;
/* Detach current tail */
listNode *tail = list->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;
}