redis中的 adlist链表实现

4 篇文章 0 订阅
2 篇文章 0 订阅
文章详细解读了Redis6.2.7版本中adlist的数据结构,包括listNode、listIter和list的定义。讲解了如何创建list,以及头插法、尾插法和指定位置插入节点的实现。同时介绍了删除节点和通过key查询节点的操作。
摘要由CSDN通过智能技术生成

adlist源码解读(基于redis 6.2.7)

1丶打开源码 adlist.h

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;  连表的长度,O(1)
} list;

​ 可见,list的结构还是比较简单的.listNode,代表每个list的结点, list整体包含了头结点,尾部结点. 而 listIter 则是在迭代操作中使用,下面具体分析.

2丶创建

list *listCreate(void)
{
    struct list *list;
	//分配空间,分配失败则返回null
    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;
}

创建比较简单,即分配内存空间. 在我的电脑64位, sizeof(list) = 48; 5个指针一个long类型 = 6 * 8=48

3丶增加节点

3.1头插法

/* Add a new node to the list, to head, containing the specified 'value'
 * pointer as value.
 * 在list中添加一个新的node,放在头部.包含一个特殊的 "value", value的指针.
 * On error, NULL is returned and no operation is performed (i.e. the
 * list remains unaltered). 
   若失败则返回NULL, 没有操作执行
 * On success the 'list' pointer you pass to the function is returned. 
 	若成功,则返回入参 传递过来的list指针
 */
list *listAddNodeHead(list *list, void *value)
{
    //新增的节点
    listNode *node;

    if ((node = zmalloc(sizeof(*node))) == NULL)
        return NULL;
    //新增加点赋值
    node->value = value;
    //list长度为0,属于刚创建的状态
    if (list->len == 0) {
        //首位节点都是这个新增的节点
        list->head = list->tail = node;
        //前后节点都是NULL;
        node->prev = node->next = NULL;
    } else {
        //新增节点的前节点置位NULL,因为是头插法,这个节点作为头结点,肯定没有前置节点.
        node->prev = NULL;
        //新增节点 后置节点 指向原来的head节点
        node->next = list->head;
        //原来的head节点,  前置节点指向 这个新增节点.
        list->head->prev = node;
        //list的头结点设置为新增加点
        list->head = node;
    }
    list->len++;
    return list;
}

3.2 尾插法

基本与头插法相似

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;
}

3.3 指定节点前后插入节点

//after为ture则向后面插入
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;
}

4丶删除节点

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--;
}

5丶查询节点

listNode *listSearchKey(list *list, void *key)
{
    listIter iter;
    listNode *node;
	//将 iter 的下一个节点设置为头结点
    listRewind(list, &iter);
    //便利
    while((node = listNext(&iter)) != NULL) {
        if (list->match) {
            if (list->match(node->value, key)) {
                return node;
            }
        } else {
            if (key == node->value) {
                return node;
            }
        }
    }
    return NULL;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SpringCloud1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值