双链表【参照redis链表结构】
参照了Redis里面的双链表结构,可以说是完全复制粘贴,redis的双链表还是写的很通俗易懂的,没有什么花里胡哨的东西,但是redis还有个iter迭代器的结构来遍历链表。我这里就没有实现了,只是实现了双链表的基本操作
redis双链表结构有如下特点
- 多态:可以储存多种数据类型
- 双端
- 无环:也就是说head->pre==NULL tail->next=NULL
- 带有长度计数器
- 有头指针和尾指针
实现
注意,这里的链表有一个迭代器结构,方便遍历链表
#include<iostream>
#include<stdlib.h>
using namespace std;
#define START_HEAD 0
#define START_TAIL 1
struct listNode{
listNode* prev;
listNode* next;
int value; //可以是void void*类型 实现多态链表
};
struct list{
listNode* head;
listNode* tail;
int len;
};
//迭代器模式
typedef struct listIter {
listNode *next;
int direction; //遍历方向
};
//创建迭代器
listIter *listGetIterator(list *myList, int direction)
{
listIter *iter = (listIter*)malloc(sizeof(listIter));
if (direction == START_HEAD)
iter->next = myList->head;
else
iter->next = myList->tail;
iter->direction = direction;
return iter;
}
/* Release the iterator memory */
void listReleaseIterator(listIter *iter) {
free(iter);
}
//将迭代器指向开头节点
void listRewind(list *myList, listIter *li) {
li->next = myList->head;
li->direction = START_HEAD;
}
//迭代器指向尾部节点
void listRewindTail(list *myList, listIter *li) {
li->next = myList->tail;
li->direction = START_TAIL;
}
//用迭代器遍历下一个节点
listNode *listNext(listIter *iter)
{
listNode *current = iter->next;
if (current != NULL) {
if (iter->direction == START_HEAD)
iter->next = current->next;
else
iter->next = current->prev;
}
return current;
}
//初始化链表
list* listCreate(){
list* myList;
myList = (list*)malloc(sizeof(list));
myList->head = myList->tail=NULL;
myList->len=0;
return myList;
}
//移除链表元素
void listEmpty(list* list){
int len = 0;
listNode* current,*next;
current = list->head;
while(len--){
next = current->next;
free(current);
current = next;
}
list->head=list->tail=NULL;
list->len=0;
}
void listRelease(list* list){
listEmpty(list);
free(list);
list = NULL;
}
//头插
list* listAddNodeHead(list* list,int val){
listNode* node = (listNode*)malloc(sizeof(listNode));
node->value = val;
if(list->len==0){
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* listAddNodeTail(list* myList,int val){
listNode* node = (listNode*)malloc(sizeof(listNode));
node->value = val;
if(myList->len==0){
myList->head = myList->tail = node;
node->prev=node->next=NULL;
}else{
node->prev = myList->tail;
myList->tail->next = node;
node->next=NULL;
myList->tail = node;
}
myList->len++;
return myList;
}
//插入到指定节点前面或后面
list* listInsertNode(list* list,listNode* old_node,int val,bool after){
listNode* node = (listNode*)malloc(sizeof(listNode));
node->value = val;
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 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;
free(node);
node = NULL;
list->len--;
}
//链表的复制
list* listCopy(list* old_list){
list* new_list = listCreate();
listNode* node;
listIter iter;
listRewind(old_list,&iter); //指向开头节点
while((node=listNext(&iter))!=NULL){
listAddNodeTail(new_list,node->value);
}
//listRelease(old_list);
return new_list;
}
//根据值查找节点
listNode* listSearchKey(list* myList,int val){
listNode* node;
listIter iter;
listRewind(myList,&iter);
while((node=listNext(&iter))!=NULL && node->value!=val)
;
if(node)
return node;
else
return NULL;
}
//根据索引查找节点 支持倒排序索引
listNode* listIndex(list* list,int index){
listNode* node;
if(index<0){
index = (-index)-1;
node = list->tail;
while(index-- && node) node =node->prev;
}
else{
node = list->head;
while(index-- && node) node = node->next;
}
return node;
}
//second连接到first
list* listJoin(list* first,list* second){
if(second->head)
second->head->prev = first->tail;
if(first->tail)
first->tail->next = second->head;
else //first链表是空的
first->head = second->head;
if(second->tail)
first->tail = second->tail;
first->len +=second->len;
second->head = second->tail = NULL;
second->len = 0;
return first;
}
#define dlist_for_each(pos, head) \
for (pos = (head)->next; pos != NULL; pos = pos->next)
#define dlist_reverse_for_each(pos, head) \
for (pos = (head)->next; pos!=NULL; pos = pos->next)
void Show_Int_List(list* myList){
listNode* node;
dlist_for_each(node,myList->head){
cout<<node->value<<endl;
}
}
int main(){
list* myList1 = listCreate();
for(int i=1;i<=5;i++)
listAddNodeTail(myList1,i);
Show_Int_List(myList1);
cout<<endl;
list* myList2 = listCreate();
for(int i=6;i<=10;i++)
listAddNodeTail(myList2,i);
Show_Int_List(myList2);
cout<<endl;
listJoin(myList1,myList2);
Show_Int_List(myList1);
cout<<endl;
cout<<listIndex(myList1,-2)->value<<endl;
cout<<endl;
list* myList3=NULL;
myList3 = listCopy(myList1);
listAddNodeHead(myList3,99);
listDelNode(myList3,listSearchKey(myList3,10));
Show_Int_List(myList3);
cout<<endl;
cout<<"len:"<<myList3->len<<endl;
return 0;
}
双链表【参照redis链表结构】的更多相关文章
- Redis数据结构—链表与字典的结构
目录 Redis数据结构-链表与字典的结构 链表 Redis链表节点的结构 Redis链表的表示 Redis链表用在哪 字典 Redis字典结构总览 Redis字典结构分解 Redis字典的使用 Re ...
- redis 链表
redis 链表 前言 借鉴了 黄健宏 的 <<Redis 设计与实现>> 一书, 对 redis 源码进行学习 欢迎大家给予意见, 互相沟通学习 概述 redis 的链表结构 ...
- Redis链表实现
链表在 Redis 中的应用非常广泛, 比如列表键的底层实现之一就是链表: 当一个列表键包含了数量比较多的元素, 又或者列表中包含的元素都是比较长的字符串时, Redis 就会使用链表作为列表键的底层 ...
- PHP+Redis链表解决高并发下商品超卖问题
目录 实现原理 实现步骤 上一篇文章聊了一下使用Redis事务来解决高并发商品超卖问题,今天我们来聊一下使用Redis链表来解决高并发商品超卖问题. 实现原理 使用redis链表来做,因为pop操作是 ...
- Redis底层结构全了解
第一篇文章,思来想去,写一写Redis吧,最近在深入研究它. 一丶Redis底层结构 1. redis 存储结构 redis的存储结构从外层往内层依次是redisDb.dict.dictht.dict ...
- 《闲扯Redis七》Redis字典结构的底层实现
一.前言 上节<闲扯Redis六>Redis五种数据类型之Hash型 中说到 Hash(哈希对象)的底层实现有: 1.ziplist 编码的哈希对象使用压缩列表作为底层实现 2.hasht ...
- Redis的结构和运作机制
目录 1.数据库的结构 1.1 字典的底层实现 2.过期键的检查和清除 2.1 定时删除 2.2 惰性删除 2.3 定期删除 2.4 对RDB.AOF和复制的影响 3.持久化机制 3.1 RDB方式 ...
- 使用C语言描述静态链表和动态链表
静态链表和动态链表是线性表链式存储结构的两种不同的表示方式. 静态链表的初始长度一般是固定的,在做插入和删除操作时不需要移动元素,仅需修改指针,故仍具有链式存储结构的主要优点. 动态链表是相对于静态链 ...
- C语言 Linux内核链表(企业级链表)
//Linux内核链表(企业级链表) #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...
随机推荐
- Android ConstraintLayout 构建自适应界面
原文链接 使用 ConstraintLayout 构建自适应界面 ConstraintLayout 可让您使用扁平视图层次结构(无嵌套视图组)创建复杂的大型布局.它与 RelativeLayout 相 ...
- Java 中的递归
递归 递归 一种通过调用某个方法来描述需要重复进行的操作.该方法的特点就是可以自己调用自己. 案例一 排队的问题 在生活中,我们经常需要排队.在排队中,我们怎么才能知道自己所排在第几位呢? 我们也许会 ...
- vue post请求 参数带有中文后端无法接收或者收到乱码,无法返回数据问题
问题来源: 在使用axios时,和java联调,发现调接口服务器始终拿不到参数data,但是检查network也的确传了data,才有了该文章. 基于 vue-axios 和 $.ajax 两种请求方 ...
- Linux 常用命令(遇见了,就记录了 ,随缘吧)
1.实时查看最后日志(默认10条) # tail -f xxxxxx.log 2.查看结尾多少条日志 # tail -n30 -f xxxx.log 3.根据关键字查询日志 # cat xxxxx.l ...
- vs整合MySQL和QT
23:37:23 2019-08-12 尝试用vs写一个程序整合MySQL和QT 参考资料:https://blog.csdn.net/qq_35987486/article/details/8406 ...
- Golang笔记集
学习Golang了, 下面分享我的, 还有我收集的Golang的学习资料 我的基础笔记地址: https://github.com/zhuchangwu/go-study-notes 其他参考: Go ...
- VSCode——自定义VSCode背景图片
本文转载自https://blog.csdn.net/yukinoai/article/details/84564949 1.以管理员身份运行VS Code,安装background插件 2.打开se ...
- Jenkins构建项目后发送钉钉消息推送
前言 钉钉是我们日常工作的沟通工具,在Jenkins构建持续集成项目配合钉钉机器人的功能,可以让我们在持续集成测试环节快速接收到测试结果的消息推送. 一:新建一个钉钉群,选择自定义机器人 二:添加机器 ...
- 14-jmeter分布式环境
1.分布式概念: jmeter做性能时,会消耗本地机器资源 本机无法没有限制的创建运行线程(一般500线程就差不多会报错) 一般这时候会用到分布式的环境 2.环境: 前提条件:环境一致(有时候可以直接 ...
- 2019-06-02 Python之微信好友数据分析以及运用Pyecharts可视化
一.库的使用说明 pass 二.微信好友信息的获取 def get_friends_info(self): #获取好像信息,返回lis列表 bot = Bot() lis = [['name', 'r ...