//adlist.h
#ifndef __ADLIST__H__
#define __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;
}List; class ListM {
public:
enum {
AL_START_HEAD = ,
AL_START_TAIL = ,
AL_START_ERR
}; static List *ListCreate(void);
static void ListRelease(List *list); static List *ListAddNodeHead(List *list, void *value);
static List *ListAddNodeTail(List *list, void *value);
static List *ListInsertNode(List *list,
listNode *old_node,
void *value,
int after); static void ListDelNode(List *list, listNode *node); static listIter *ListGetIterator(List *list, int direction);
static listNode *ListNext(listIter *iter);
static void ListReleaseIterator(listIter *iter); static List *ListDup(List *orig);
static listNode *ListSearchKey(List *list, void *key);
static listNode *ListIndex(List *list, long index); static void ListRewind(List *list, listIter *li);
static void ListRewindTail(List *list, listIter *li);
static void ListRotate(List *list); static void Traversal(List *list);
}; #endif //__ADLIST__H__
 //adlist.cpp
#include <stdlib.h>
#include <iostream>
#include "adlist.h"
#include "zmalloc.h" List* ListM::ListCreate(void) {
List *list;
if((list = (List *)zmalloc(sizeof(List))) == NULL)
return NULL; list->head = list->tail = NULL;
list->len = ;
list->dup = NULL;
list->free = NULL;
list->match = NULL;
return list;
} void ListM::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);
} List *ListM::ListAddNodeHead(List *list, void *value) { listNode *node; if((node = (listNode *)zmalloc(sizeof(*node))) == NULL) {
return NULL;
}
node->value = value; if(list->len == ) {
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;
} List *ListM::ListAddNodeTail(List *list, void *value) { listNode *node; if((node = (listNode *)zmalloc(sizeof(*node))) == NULL) {
return NULL;
} node->value = value;
if(list->len == ) {
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;
} List *ListM::ListInsertNode(List *list, listNode *old_node, void *value,int after) {
listNode *node; if((node = (listNode *)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;
} void ListM::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--;
} listIter *ListM::ListGetIterator(List *list, int direction) { listIter *iter; if((iter = (listIter *)zmalloc(sizeof(*iter))) == NULL) return NULL; if(direction == AL_START_HEAD)
iter->next = list->head;
else
iter->next = list->tail;
iter->direction = direction; return iter;
} listNode *ListM::ListNext(listIter *iter) {
listNode *current = iter->next; if(current != NULL) {
if(iter->direction == AL_START_HEAD)
iter->next = current->next;
else
iter->next = current->prev;
}
return current;
} void ListM::ListReleaseIterator(listIter *iter) {
zfree(iter);
} listNode *ListM::ListSearchKey(List *list, void *key) {
listNode *node = NULL;
listIter *iter = NULL; iter = ListGetIterator(list, AL_START_HEAD);
while((node = ListNext(iter)) != NULL) {
if(list->match) {
if(list->match(node->value, key)) {
ListReleaseIterator(iter);
return node;
}
} else {
if(key == node->value) {
ListReleaseIterator(iter);
return node;
}
}
} ListReleaseIterator(iter);
return NULL;
} listNode *ListM::ListIndex(List *list, long index) {
listNode *node = NULL; if(index < ) {
index = (-index) - ;
node = list->tail;
while(index-- && node) node = node->prev;
} else {
node = list->head;
while(index-- && node) node = node->next;
}
return node;
} void ListM::ListRewind(List *list, listIter *li) {
li->next = list->head;
li->direction = AL_START_HEAD;
} void ListM::ListRewindTail(List *list, listIter *li) {
li->next = list->tail;
li->direction = AL_START_TAIL;
} void ListM::ListRotate(List *list) {
listNode *tail = list->tail; if(list->len <= ) return; list->tail = tail->prev;
list->tail->next = NULL; list->head->prev = tail;
tail->prev = NULL;
tail->next = list->head;
list->head = tail;
} void ListM::Traversal(List *list) {
if(list->head == list->tail ) return; listNode *node = NULL;
listIter *iter = ListGetIterator(list, AL_START_HEAD);
std::cout << "list data : ";
while((node = ListNext(iter)) != NULL) {
std::cout << *(int*)node->value << "\t";
}
std::cout << std::endl;
}
 //main.cpp
#include <iostream>
#include "adlist.h" int main() { List *l = ListM::ListCreate(); int value = ;
ListM::ListAddNodeHead(l, (void*)&value); int value2 = ;
ListM::ListAddNodeHead(l, (void*)&value2); int value3 = ;
ListM::ListAddNodeTail(l, (void*)&value3); ListM::Traversal(l); return ;
}

redis下的adlist的更多相关文章

  1. redis下操作hash对象

    redis下操作hash对象 hash用于存储对象,对象的格式为键值对 命令 设置 设置单个属性 HSET key field value 设置多个属性 HMSET key field value [ ...

  2. Redis源代码-数据结构Adlist双端列表

    Redis的Adlist实现了数据结构中的双端链表,整个结构例如以下: 链表节点定义: typedef struct listNode { struct listNode *prev; struct ...

  3. 安装redis出现cc adlist.o /bin/sh:1:cc:not found的解决方法

    安装redis时 提示执行make命令时提示 CC adlist.o /bin/sh: cc: 未找到命令   问题原因:这是由于系统没有安装gcc环境,因此在进行编译时才会出现上面提示,当安装好gc ...

  4. 安装redis出现cc adlist.o /bin/sh:1:cc:not found

    安装redis时 提示执行make命令时, 提示 CC adlist.o /bin/sh: cc: 未找到命令 问题原因:这是由于系统没有安装gcc环境,因此在进行编译时才会出现上面提示,当安装好gc ...

  5. redis下操作Set和Zset

    redis操作set 无序集合 元素为string类型 元素具有唯一性,不重复 命令 设置 添加元素 SADD key member [member ...]  获取 返回key集合所有的元素 SME ...

  6. redis下操作String

    redis操作string string是redis最基本的类型 最大能存储512MB数据 string类型是二进制安全的,即可以为任何数据,比如数字.图片.序列化对象等 基本命令 设置 设置键值 s ...

  7. redis下的字符串处理

    redis设计一款sds对象[字符串对象] 优点:可跨平台的内存处理zmalloc:内存消耗的线性增长优势:每次加SDS_MAX_PREALLOC(1MB)的空间: 重写了各种字符串操作的函数: 写跨 ...

  8. redis 下key的过期时间详解 :expire

    memcached 和 redis 的set命令都有expire参数,可以设置key的过期时间.但是redis是一个可以对数据持久化的key-value database,它的key过期策略还是和me ...

  9. redis下操作列表list

    list 列表的元素类型为string 按照插入顺序排序 在列表的头部或者尾部添加元素 命令 设置 在头部插入数据 LPUSH key value [value ...] 在尾部插入数据 RPUSH ...

随机推荐

  1. c++ 沉思录---代理类

    一.问题 如何设计一容器能包含彼此不同而又相互关联的类的对象(处于完整的继承层次的类)?因为一般的数组容器都只能包含一种类型的对象. 假设有一个表示不同类型的交通工具的类的派生层次: class Ve ...

  2. 使用JS传递数组型数据回服务器

    //为数组添加一个方法,判断某个值是否存在于数组中 Array.prototype.in_array = function (e) { for (i = 0; i < this.length & ...

  3. Python制作回合制手游外挂简单教程(中)

    接着上篇的博文,今天我们讲如何实现自动组队刷道 引入: 自动组队刷道的流程是先点击刷道按钮.再点击前往按钮.再点击便捷组队······ 这些操作上篇博文已经告诉我们怎么做了,利用picpick丈量坐标 ...

  4. Eclipse空白包的显示和隐藏

    Eclipse空白包的显示和隐藏 点击三角形, ,下拉 -> Customize View... -> Empty packages (勾选)

  5. 【angular5项目积累总结】遇到的一些问题以及解决办法

    1.项目中字符串特别是\r\n,替换成br之后,在页面换行无法生效? 答:绑定元素 innerHTML. <div class="panel-body" [innerHTML ...

  6. developer.android.google.cn

    Android Studio官方 Android IDE https://developer.android.google.cn/studio/index.html 探索 Android Studio ...

  7. 设置tomcat字符编码

    Tomcat的默认编码是ISO-8859-1,如果有是get请求时,会出现乱码,这种情况可以修改Tomcat的编码解决,当然也可以写个过滤器来解决. 在tomcat的conf目录下,编辑server. ...

  8. 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用

    使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...

  9. LINQ-Where子句与select子句

    1.Where子句 where子句的作用就是筛选元素,除了开始喝结束位置,where子句几乎可以出现在LINQ表达式的任意位置.一个LINQ表达式中可以有Where子句,也可以没有:可以有一个,可以有 ...

  10. 浏览器获取正确的scrollTop值

    window.pageYOffset 被所有浏览器支持除了 IE 6, IE 7, IE 8, 不关doctype的事, 注IE9 开始支持此属性. window.scrollY 被Firefox, ...