redis 之 adlist

digraph adlist {      rankdir=LR;      node [shape=record, style = filled, fillcolor = "#95BBE3"];      edge [style = bold];      list_node_1 [label = "<head>listNode |{<prev> prev| value|<next> next}", ];     list_node_2 [label = "<head>listNode |{<prev> prev| value|<next> next}"];     list_node_3 [label = "<head>listNode |{<prev> prev| value|<next> next}"];      list_node_1:next -> list_node_2:head;     list_node_2:next -> list_node_3:head;      list_node_2:prev -> list_node_1:head;     list_node_3:prev -> list_node_2:head;      node [width=1.5, style = filled, fillcolor = "#A8E270"];     list [label = "list |<head> head|<tail> tail|<dup> dup|<free> free|<match> match|<len> len"];      list:tail -> list_node_3:head;     list:head -> list_node_1:head; }

这里主要是测试和学习使用了 adlist 的相关函数, 具体原理后面补充:

1.  拷贝 :

adlist.c  和 adlist.h  到一个单独的文件夹, 自己创建一个 main.c 的文件

# 我的目录文件结构
adlist  adlist.c  adlist.h  main.c

#adlist 是编译后生成的可执行文件
#adlist.c 是 redis 源码里面的 adlist.c 做了少许修改
#adlist.h 是 redis 源码里面的 adlist.h 没有做任何修改
#main.c 为测试代码程序

adlist.c 修改部分如下:

#include <stdlib.h>
#include "adlist.h"
//#include "zmalloc.h"  // 注释调该头文件

// 定义了下面的两个宏
#define zmalloc malloc
#define zfree free

这里不贴 adlist.c 和 adlist.h 文件的内容了, 具体可以从 redis 里面的源码获得。

2. 具体测试源码如下:

#include <stdio.h>
#include <stdlib.h>
#include "adlist.h"

char str[] = "ABCDEFGHIJKLMOPQRSTUVWXYZ";

int main(int argc, char *argv[])
{
    int alphLen = sizeof(str)/sizeof(char);
    list *lst = listCreate();
    if (lst == NULL) {
        printf("listCreate() error!\n");
        return -1;
    }
    
    int i = 0;
    for (i = 0; i < alphLen; i++) {
       lst = listAddNodeHead(lst, &str[i]);
    }

    listIter *item = listGetIterator(lst, AL_START_HEAD);
    listNode *node = NULL;
    while ((node = listNext(item)) != NULL) {
        char *p = listNodeValue(node);
        printf("%c ", (char)(*p));
    }
    printf("\n");

    
    listRewindTail(lst, item);
    while ((node = listNext(item)) != NULL) {
        char *p = listNodeValue(node);
        printf("%c ", (char)(*p));
    }
    printf("\n");
    
    listReleaseIterator(item);
    item = NULL;

    printf("------------------------------------------------\n");
    list *lstdup = listDup(lst);

    listIter *itemdup = listGetIterator(lstdup, AL_START_TAIL);
    listNode *nodedup = NULL;
    while ((nodedup = listNext(itemdup)) != NULL) {
        char *p = listNodeValue(nodedup);
        printf("%c ", (char)(*p));
    }
    printf("\n");
    
    listRewind(lstdup, itemdup);
    while ((nodedup = listNext(itemdup)) != NULL) {
        char *p = listNodeValue(nodedup);
        printf("%c ", (char)(*p));
    }
    printf("\n");
    listReleaseIterator(itemdup); 
    itemdup = NULL;

    printf("------------------------------------------------\n");
    for (i = 0; i < alphLen; i++)  {
        listNode *indexN = listIndex(lst, (long)(i));
        char *p = listNodeValue(indexN);
        printf("%d -> %c\n", i, (char)(*p));
    }

    printf("------------------------------------------------\n");
    listRotate(lst);
    listNode *index0 = listIndex(lst, (long)(0));
    char *p = listNodeValue(index0);
    printf("%d -> %c\n", 0, (char)(*p));

    printf("------------------------------------------------\n");
    listDelNode(lst, index0); 
    listIter it;
    listRewindTail(lst, &it);
    while ((node = listNext(&it)) != NULL) {
        char *p = listNodeValue(node);
        printf("%c ", (char)(*p));
    }
    printf("\n");
    printf("------------------------------------------------\n");
    
    listNode *index3 = listIndex(lst, (long)(3));
    lst = listInsertNode(lst, index3, &str[0], 1);
    listRewind(lst, &it);
    while ((node = listNext(&it)) != NULL) {
        char *p = listNodeValue(node);
        printf("%c ", (char)(*p));
    }
    printf("\n");
    printf("------------------------------------------------\n");
    
    
    listRelease(lst);
    listRelease(lstdup);
    return 0;
}


3. 编译:

gcc main.c adlist.c -o adlist

4. 输出:

./adlist 
 Z Y X W V U T S R Q P O M L K J I H G F E D C B A 
A B C D E F G H I J K L M O P Q R S T U V W X Y Z  
------------------------------------------------
A B C D E F G H I J K L M O P Q R S T U V W X Y Z  
 Z Y X W V U T S R Q P O M L K J I H G F E D C B A 
------------------------------------------------
0 -> 
1 -> Z
2 -> Y
3 -> X
4 -> W
5 -> V
6 -> U
7 -> T
8 -> S
9 -> R
10 -> Q
11 -> P
12 -> O
13 -> M
14 -> L
15 -> K
16 -> J
17 -> I
18 -> H
19 -> G
20 -> F
21 -> E
22 -> D
23 -> C
24 -> B
25 -> A
------------------------------------------------
0 -> A
------------------------------------------------
B C D E F G H I J K L M O P Q R S T U V W X Y Z  
------------------------------------------------
 Z Y X A W V U T S R Q P O M L K J I H G F E D C B 
------------------------------------------------

 

转载于:https://my.oschina.net/tsh/blog/1486559

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值