//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. android studio全局搜索关键字

  2. [转]log4net 发布到生产环境不写日志的解决方法--使用 NLog日志

    本文转自:http://www.cnblogs.com/weiweictgu/p/5848805.html 1.升级到log4net的最新版 PM下执行 Install-Package log4net ...

  3. 怎么让一段xml被识别为字符串

    public static String decodeString(String strData) { strData = replaceString(strData, "<" ...

  4. 使用NPOI,完成数据的导入导出

    解释下流程,第一步:将数据库表中的数据导出到excel表                          第二步:将excel表中的数据再插入到数据库表中(当然没有做重复性校验,测试而已)注:表结构 ...

  5. 面向对象(基础oop)之进入继承

    大家好,我叫李京阳,,很高兴认识大家,之所以我想开一个自己的博客,就是来把自己所了解的知识点通过自己的话写一下,希望被博客园的朋友们点评和一起讨论一下,也希望从博客园中多认识一些软件开发人员!现在我开 ...

  6. SpringBoot 之Quartz的使用

    对于Quartz的使用,还是想说一句,SpringBoot真的很好用啊! 第一步:当然是引入依赖啦 <parent> <groupId>org.springframework. ...

  7. 关于一次美团java程序员招聘面试的经历

    美团一面: 中间省略掉大概几个问题,因为我不记得了,下面记得的基本都是我没怎么答好的. 1.了解SOA,微服务吗? 2.分布式系统如何负载均衡?如何确定访问的资源在哪个服务器上? 一.轮询.二.随机. ...

  8. Springboot简介01

    前言: spring是近几年java中最具有代表而且最为流行的框架,spring是基于aop和IOC的思想,在我们的项目中充当了一个粘合剂的作用,既可以成为对象工厂,来管理我们的controller. ...

  9. springboot的依赖注入报null的问题

    最近使用springboot开发项目,使用到了依赖注入,频繁的碰到注入的对象报空指针,错误如下 java.lang.NullPointerException: null at com.mayihc.a ...

  10. jenkins 参数化构建过程

    构建项目时我们可能需要切换到另一个分支编译,或者说每次编译版本都要加1,这时候我们可以改配置或者改脚本文件,这显然不是一个好的方式,那么如何能在编译前让用户输入参数呢?jenkins早就为我们考虑好 ...