//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. 如果天空不死博客java阅读列表整理

    如果天空不死的主页https://home.cnblogs.com/u/skywang12345 下面是最近总结的Java集合(JDK1.6.0_45)相关文章的目录. 01. Java 集合系列01 ...

  2. Jenkins 中创建项目时没有Maven项目怎么办

    如果在创建项目时候,没有“创建一个Maven 项目”的选项. 你需要安装Maven项目插件:Maven Integration plugin . 点击“可选插件”  然后在右边的过滤输入框中输入搜索关 ...

  3. JAVA练手--String

    package tet; public class kk { public static void main(String[] args) { //1. { String Stra = "1 ...

  4. Web前端学习资料

    http://www.imooc.com/course/list?c=html http://www.w3cplus.com/ http://www.w3cfuns.com/ http://www.w ...

  5. vs2015中的数据库架构对比工具(New Schema Comparison)

    大家都知道VS里的功能多到你根本没用过,今天来说说这个New Schema Comparison,他能做的事情就是在vs中对比我们两个数据库的架构.结构,并且能够更新过去或者生成脚本. Step.1( ...

  6. nodejs设置服务端允许跨域

    //设置跨域访问 app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", ...

  7. [javaSE] GUI(Action事件)

    对自己定义的类规范化一下,事件和图形化组件分离出来 定义一个类FrameDemo 定义成员属性Frame frame 定义成员属性Botton 定义构造方法FrameDemo() 定义初始化方法ini ...

  8. Java学习--jsp内置对象

    九个内置对象,其中Out,request,response,session,application常用 get与post区别: request对象: response对象: 请求转发与请求重定向的区别 ...

  9. Google Voice号码使用说明及用途

    Google Voice号码使用说明及用途 号码 已不能网页注册获取,直接向TB购买,搜Google Voice就行了.很便宜的. 一. Google Voice介绍 Google Voice首先是一 ...

  10. SSM maven框架下载简易版

    1.前台一个a标签,写个地址就行了 例如 <a href="${pageContext.request.contextPath}/fileDownLoad">前去下载& ...