redis 链表

typedef struct listNode {
    struct listNode *prev;				//先前节点
    struct listNode *next;				//后续节点
    void *value;						//存储value
} listNode;
typedef struct list {
    listNode *head;					//head
    listNode *tail;						//tail
    void *(*dup)(void *ptr);
    void (*free)(void *ptr);
    int (*match)(void *ptr, void *key);
    unsigned long len;
} list;
//创建list
list *listCreate(void)
{
    struct list *list;

    if ((list = zmalloc(sizeof(*list))) == NULL)		//malloc分配
        return NULL;
    list->head = list->tail = NULL;				//head和tail都置空
    list->len = 0;								//长度0
    list->dup = NULL;							//dup 函数
    list->free = NULL;							//value 的 free 函数
    list->match = NULL;						//match函数
    return list;
}
//释放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);
}
//添加加点,插入到head后面
list *listAddNodeHead(list *list, void *value)
{
    listNode *node;
//申请链表node
    if ((node = zmalloc(sizeof(*node))) == NULL)
        return NULL;
//value 赋值
node->value = value;
//insert
    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;
}
listAddNodeTail 类似
//插入节点
list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
    listNode *node;

    if ((node = zmalloc(sizeof(*node))) == NULL)
        return NULL;
node->value = value;
//如果是after
    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--;
}
typedef struct listIter {
    listNode *next;
    int direction;
} listIter;	//迭代器,使用也就是next 依次遍历

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值