//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. VMware虚拟机配置

    VMware虚拟机配置 背景 在做学习Linux时经常会在win环境下安装虚拟机,这其中涉及到一些细节操作,需要对虚拟机工作模式加以理解. 本文在学习hadoop分布式环境搭建时写作. 1.虚拟交换机 ...

  2. WPF解决当ScrollViewer中嵌套ItemsControl时,不能使用鼠标来滚动翻页

    1. ScrollViewer:滚动条容器,当内容超过指定的长度和宽度后,就会出现滚动条,而且可以使用鼠标中键来滚动, 简单例子如下: <Window x:Class="Connect ...

  3. 【总结】java 后台文件上传整理

    public Map<String,String> clientUploadAttachment(Long belongId, String fileSource, MultipartFi ...

  4. 跟着Nisy一起学习C语言

    编辑器是使用环境turboc的IDE,使用dos窗口中的edit作为编辑器,有点类似于vim:使用的是xp-sp3的虚拟机上的系统. Nisy说要有两种语言,脚本语言以及一个底层语言,比如现在我的py ...

  5. DataGridView 获取当前单元格

    获取DataGridview控件中的当前单元格,是通过DataGridview的Rows属性和Column属性的索引来取得的,他们的索引都是从0开始的. Private void datagridvi ...

  6. LINQ操作List<T>

    LINQ操作List<T>主要包括: 1.筛选 List<string> stcdList = stcdArray.ToList<string>() .FindAl ...

  7. 在 Azure Web 应用中创建 .NET 应用程序

    本快速入门帮助你在数分钟内将你的第一个 ASP.NET Web 应用部署到 Azure 应用服务.完成本教程后,你将能够在云中启动并运行一个简单的 Web 应用.在本教程中完成的所有操作均符合1 元试 ...

  8. [javaSE] IO流(管道流)

    之前我们使用io流,都是需要一个中间数组,管道流可以直接输入流对接输出流,一般和多线程配合使用,当读取流中没数据时会阻塞当前的线程,对其他线程没有影响 定义一个类Read实现Runable接口,实现r ...

  9. 记一次tomcat自动退出问题

    问题 环境: centos/tomcat8/jdk1.8 最近遇到部署在服务器的tomcat总是过一段时间就自动结束进程 ; 通过监控tomcat 日志文件(tail -f ./logs/catali ...

  10. 【SSH网上商城项目实战25】使用java email给用户发送邮件

       转自: https://blog.csdn.net/eson_15/article/details/51475046 当用户购买完商品后,我们应该向用户发送一封邮件,告诉他订单已生成之类的信息, ...