这里主要是测试和学习使用了 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
------------------------------------------------