潍坊市论坛

首页 » 分类 » 问答 » Redis源码adlist学习之三ad
TUhjnbcbe - 2021/8/19 18:00:00
北京专业白癜风医院 http://baidianfeng.39.net/
Redis源码adlist学习之三-adlist存字符串

本系列文章阅读的源码都是Redis-3.0.3版本,编译时使用的是libc库。

adlist的上两篇文章,讲解了Redis中双向链表adlist的建立,头插尾插结点,删除结点,查找结点等函数,当时只是结点存储为int的简单值进行函数验证,本篇文章将使链表结点存储字符串,比存储int稍微复查一点,并将上述函数都一块进行验证,也会介绍几个宏的使用,编写综合代码尽量验证到adlist的常用函数。

验证代码如下:

#includestdio.h#include"../adlist.h"#defineARR_LEN6intCompareAdlistStr(void*ptr,void*key){returnstrcmp(ptr,key)==0?1:0;}voidprintAdlistNode(list*pphead,intdirection){listIter*pIter1=listGetIterator(pphead,AL_START_HEAD);listNode*tempnode=listNext(pIter1);while(tempnode){printf("node--:%pvalue--:%s\n",tempnode,(char*)(tempnode-value));tempnode=listNext(pIter1);}listReleaseIterator(pIter1);}intmain(intargc,char*argv[]){chararrayadlist[ARR_LEN][20]={"hello","smart","Iloveyou!","BestRegards!","","OK!"};structlist*phead=listCreate();inti=0;for(i=0;iARR_LEN;i++){if(i%2)//奇数listAddNodeTail(phead,arrayadlist);elselistAddNodeHead(phead,arrayadlist);}//printf("Printeachnodeaddressandvalue:\n");//轮询输出各链表值//structlistNode*ptemp=phead-tail;//while(ptemp){//printf("node:%p,value--:%s\n",ptemp,(char*)(ptemp-value));//ptemp=ptemp-prev;//}printf("/////DisplayAdlist//////\n");printAdlistNode(phead,AL_START_HEAD);printf("listlength:%d\n",listLength(phead));listSetMatchMethod(phead,CompareAdlistStr);//设置match指针函数,比对字符串printf("\n-----Searchvalueinadlist-----\n");char*pSearchValue="BestRegards!";printf("Willsearchthestring:%s\n",pSearchValue);listNode*pSearch=listSearchKey(phead,pSearchValue);printf("Foundthenode:%p\n",pSearch);printf("\n-----Insertanewnodeinadlist-----");char*pNewNode="I

1