redis源码阅读-数据结构篇-双端列表adlist

本文详细介绍了双端链表的数据结构定义及实现方法,包括节点和链表的定义、构造和析构函数、迭代器使用、节点增删查改等核心操作,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3. 双端链表 adlist.h 和 adlist.c

数据结构定义
  • 节点

    // 节点
    typedef struct listNode {
        struct listNode *prev;
        struct listNode *next;
        void *value;
    } listNode;
    
  • 链表

    // 链表
    typedef struct list {
        listNode *head;
        listNode *tail;
        unsigned long len;
        // 节点值复制、释放和对比函数,用于用户自定义,否则直接操作value指针
        void *(*dup)(void *ptr);
        void (*free)(void *ptr);
        int (*match)(void *ptr, void *key);
    } list;
    
  • 迭代器

    // 迭代器
    typedef struct listIter {
        listNode *next; 	// 当前节点
        int direction; 		// 迭代的方向
    } listIter;
    
  • 部分宏定义

    // some macros
    #define listLength(l) 			((l)->len)
    #define listSetDupMethod(l,m) 	((l)->dup = (m))
    ...
    
构造函数
// 创建空链表,内存不足返回NULL
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;
}
析构函数
// 释放链表,会释放所有节点指针和链表指针,可调用自定义函数
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);
}
get iter
// 创建新迭代器
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;
}
delete iter
// 释放迭代器
void listReleaseIterator(listIter *iter) {
    zfree(iter);
}
reset iter
// 重置迭代器
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;
}
迭代
// 获取当前节点
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;
}
add node to head
// 添加节点到表头,到表尾同理
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;
}
insert node
// 插入新节点到 old_node 之前或之后
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;
}
delete node
// 删除 node 节点,可调用自定义函数
void listDelNode(list *list, listNode *node) {
    // 更新 prev 节点
    if (node->prev)
        node->prev->next = node->next;
    else
        list->head = node->next;
    // 更新 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--;
}
get node by key
// 匹配节点 value 和 key,可调用自定义函数,否则比较指针
// 成功返回第一个
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;
}
get node by index
// 返回第 index 个节点
// 可为负数,-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;
}
copy list
// copy 新链表,可调用自定义copy,否则浅拷贝
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;
}
rotate
// 循环右移一位
void listRotate(list *list) {
    listNode *tail = list->tail;
    if (listLength(list) <= 1) return;
    // 更新表尾
    list->tail = tail->prev;
    list->tail->next = NULL;
    // 更新自身指针
  	tail->prev = NULL;
    tail->next = list->head;
    // 更新表头
    list->head->prev = tail;
    list->head = tail;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值