游戏开发之使用类封装双链表数据结构及双链表迭代器实现初版(C++基础)

1.数据结构及实现如下

#include <iostream>
#include <functional>
//避免二义性
typedef void* PVOID;
class DoubleList
{private://外部应无法访问tagDoubleListNode的存在,即不知道存在tagDoubleListNode子类typedef struct tagDoubleListNode{PVOID v;//节点数据地址struct tagDoubleListNode *prior;//前驱节点struct tagDoubleListNode *next;//后继节点}TDoubleListNode, *PTDoubleListNode;PTDoubleListNode _nStart;//记录头节点PTDoubleListNode _nEnd;//记录尾节点int _size;//当前链表节点数目public://初始化列表初始化DoubleList() :_nStart(NULL), _nEnd(NULL), _size(-1) {}//析构~DoubleList(){PTDoubleListNode headNode = _nStart;while (headNode != NULL){headNode = headNode->next;delete _nStart;_nStart = headNode;}headNode = NULL;}//拷贝构造函数explicit DoubleList(const DoubleList& doubleList){PTDoubleListNode Node = doubleList._nStart;while (Node != NULL){this->DoubleListAdd(Node->v);Node = Node->next;}}//正向迭代器class iterator{private:PTDoubleListNode _pStart;//记录链表头节点int _index;//当前迭代器所在位置public://构造函数iterator(){_pStart = NULL;_index = 0;}//构造函数重载iterator(PTDoubleListNode pStart_, int index_){_pStart = pStart_;_index = index_;}//重载!=运算符const bool& operator!=(const iterator& iter){return _index != iter._index - 1 ? 1 : 0;}//重载前置++运算符const iterator& operator++(){_index++;_pStart = _pStart->next;return *this;}//重载后置++运算符const iterator operator++(int){iterator iter;iter._index = _index;iter._pStart = _pStart;_index++;_pStart = _pStart->next;return iter;}//重载*运算符PVOID operator*(){return _pStart->v;}};//获取头部迭代器iterator begin(){return iterator(_nStart, 0);}//获取末尾节点的下一个节点iterator end(){return iterator(_nStart, _size + 1);}//反向迭代器,同上class reIterator{private:PTDoubleListNode _pEnd;int _index;public:reIterator(){_pEnd = NULL;_index = 0;}reIterator(PTDoubleListNode pEnd_, int index_){_pEnd = pEnd_;_index = index_;}const bool& operator!=(const reIterator& iter){return _index != iter._index + 1? 1 : 0;}const reIterator& operator++(){_index--;_pEnd = _pEnd->prior;return *this;}const reIterator operator++(int){reIterator iter;iter._index = _index;iter._pEnd = _pEnd;_index++;_pEnd = _pEnd->prior;return iter;}PVOID operator*(){return _pEnd->v;}};reIterator reBegin(){return reIterator(_nEnd, _size);}reIterator reEnd(){return reIterator(_nEnd, -1);}//尾插入int DoubleListAdd(PVOID const v){PTDoubleListNode pvNode = (PTDoubleListNode)new TDoubleListNode();pvNode->v = v;if (_size == -1){pvNode->prior = NULL;_nStart = pvNode;_nEnd = _nStart;}else{pvNode->prior = _nEnd;_nEnd->next = pvNode;_nEnd = pvNode;}_size++;pvNode = NULL;return 1;}//任意插入int DoubleListPush(const int index, PVOID const v){if (index > _size || index < 0)return 0;if (index + 1 == _size){DoubleListAdd(v);return 1;}PTDoubleListNode pvNode = new TDoubleListNode();pvNode->v = v;if (index == 0){pvNode->prior = NULL;pvNode->next = _nStart;_nStart = pvNode;}else{PTDoubleListNode headNode = _nStart;int count = -1;while (headNode != NULL && (++count) != index)headNode = headNode->next;pvNode->next = headNode->next;headNode->next->prior = pvNode;headNode->next = pvNode;pvNode->prior = headNode;headNode = NULL;}_size++;return 1;}//任意删除int DoubleListDelete(const int index){if (index > _size || _size == -1 || index < 0)return 0;int count = -1;PTDoubleListNode headNode = _nStart;PTDoubleListNode temp = NULL;while (headNode != NULL && (++count) != index && index != 0)headNode = headNode->next;if (index == _size){delete headNode->next;headNode->next = NULL;}else if (index == 0){temp = _nStart;_nStart = temp->next;delete temp;temp = NULL;}else{temp = headNode->next;headNode->next->next->prior = headNode;headNode->next = headNode->next->next;delete temp;temp = NULL;}_size--;return 1;}//从左往右打印int LeftPrintData(const std::function<void(void *)> const func){if (_size == 0)return 0;PTDoubleListNode Node = _nStart;while (Node != NULL){//printf("%4d", Node->v);func(Node->v);Node = Node->next;}printf("%\n");return 1;}//从右往左打印int RightPrintData(const std::function<void(void *)> const func){if (_size == 0)return 0;PTDoubleListNode Node = _nEnd;while (Node != NULL){//printf("%4d", Node->v);func(Node->v);Node = Node->prior;}printf("%\n");return 1;}
};

2.测试数据如下

int main()
{std::function<void(void*)> fPrint = [](void* pv){printf("%5d", *(int*)pv);};DoubleList doubleList;int num[10] = { 10,19,5,1,60,20,8,9,11,100 };for (int i = 0; i < 10; i++)doubleList.DoubleListAdd(num + i);doubleList.LeftPrintData(fPrint);int temp = 20;doubleList.DoubleListPush(10, &temp);doubleList.LeftPrintData(fPrint);doubleList.DoubleListDelete(0);doubleList.LeftPrintData(fPrint);DoubleList doubleList1(doubleList);doubleList1.LeftPrintData(fPrint);printf("----------------------------------------------------------\n");DoubleList::iterator iterBegin = doubleList1.begin();DoubleList::iterator iterEnd = doubleList1.end();for (; iterBegin != iterEnd; ++iterBegin)std::cout << *(int *)(*iterBegin) << std::endl;printf("----------------------------------------------------------\n");DoubleList::reIterator reIterBegin = doubleList1.reBegin();DoubleList::reIterator reIterEnd = doubleList1.reEnd();for (; reIterBegin != reIterEnd; ++reIterBegin)std::cout << *(int *)(*reIterBegin) << std::endl;return 0;
}

游戏开发之使用类封装双链表数据结构及双链表迭代器初版(C++基础)相关推荐

  1. Java游戏开发——飞行射击类游戏

    使用到的素材文件夹: 素材说明:bg0.jpg是背景图片.bomb_enemy是敌机爆炸时按顺序播放的四帧图片,bullet_0.png是子弹图片,enemy_alive.png是敌机图片,playe ...

  2. Cocos2d-x怪物智能AI怪物也有智商--之游戏开发《赵云要格斗》(6) cocos2dx 3.3移植版

    源码:git@github.com:baidang201/ARPG_Zhaoyun.git 本文将主要来讲讲游戏开发中的怪物智能,一个好的游戏一般怪物都要分等级,这样我们游戏玩起来才有意思,怪物如果智 ...

  3. Cocos2d-x血条跟随怪物运动--之游戏开发《赵云要格斗》(5)cocos2dx 3.3移植版

    源码:git@github.com:baidang201/ARPG_Zhaoyun.git   本章在前面的基础上<Cocos2d-x自定义血条及其美化--之游戏开发<赵云要格斗>( ...

  4. 泊松回归与类泊松回归(《R语言实践(第二版)》)

    泊松回归与类泊松回归 0. 背景介绍 0.1 泊松回归: 0.2 本示例要做的事: 1.导包 2.导入数据 3.作图 4. 拟合泊松回归: 4.1解释模型参数 4.2 检验过度离势(Overdispe ...

  5. SDL农场游戏开发 4.Crop类,作物的产生及成长

    首先,先创建一个Entity类.该类的内部有一个精灵对象及相关操作来专门负责显示,以后需要显示的类都可继承自Entity类.比如Crop类的父类就是Entity. 问:为什么Soil类不继承自Enti ...

  6. SDL农场游戏开发 3.StaticData类

    前面说过,StaticData类负责管理程序在运行过程中不会发生变化的数据,如下为Resources目录结构: data文件夹保存着一些静态数据,比如crops.csv文件保存着作物信息,soils. ...

  7. 开发日记-20190724 关键词 读书笔记《Linux 系统管理技术手册(第二版)》DAY 14

    E6.3 隐藏口令文件的目的是什么? 通过将配置文件放置到用户的主目录中,可以对一些命令和工具进行定制.启动文件一般都是以圆点(.)开头,以字母rc结尾,这是CTSS操作系统留下的"遗迹&q ...

  8. 开发日记-20190827 关键词 读书笔记《Unix环境高级编程(第二版)》DAY 3

    Referred Blogs 文件描述符标志,文件状态标志 Linux中文件描述符fd和文件指针flip的理解 Linux编程–文件描述符fd 文件共享 Unix支持在不同进程间共享文件. 内核使用了 ...

  9. 开发日记-20190822 关键词 读书笔记《Unix环境高级编程(第二版)》《掌控习惯》DAY 2

    Preface 话说,昨天开始尝试着去改变自己,从基础的习惯开始,11:30准时睡觉,平时差不多12:30才睡觉.按理说,比平时早了一个小时睡觉吧,然后我就把闹钟提前了45分钟,想着还能比平常多睡15 ...

  10. 开发日记-20190816 关键词 读书笔记《Linux 系统管理技术手册(第二版)》DAY 24

    DNS是一个分布式数据库."分布式"意味着我的网店保存着有关我的计算机的数据,您的网店保存着有关您的计算机的数据,当某个网点需要查看另一个网点的数据时,我们大家的网店就以某种方式自 ...

最新文章

  1. Error: Discrete value supplied to continuous scale
  2. linux yum lamp环境,linux centos yum安装LAMP环境
  3. Oracle EBS PO 接受入库
  4. Boost.Test 断言的介绍
  5. 贷款时,如何评估借款人的还款意愿
  6. [转]Android限制只能在主线程中进行UI访问的实现原理
  7. 拓端tecdat|R语言多分类logistic逻辑回归模型在混合分布模拟个人风险损失值评估的应用
  8. python list 分批batch
  9. PSFTP工具传输文件的命令使用方法
  10. scrapy 官方中文文档地址
  11. 10个在工作中常用的表格函数
  12. 联想集团大裁员:“公司不是家” 和 “柳传志的回应”(
  13. 【技巧】如何给CSDN上的每篇原创文章添加版权声明
  14. 系列推荐 |《最强的 VLC 多媒体开发教程》
  15. 微信公众号接口调试流程
  16. App ios 消息推送
  17. 爱创课堂每日一题第五十六天-对前端界面工程师这个职位是怎么样理解的?它的前景会怎么样?...
  18. android 9 8 对比,iPhone 8plus在安卓机中相当于什么层次?看看它与小米9的对比吧...
  19. 【FPGA】8B/10B编码--转自wiki百科
  20. 学校校园学生寝室管理查寝打分系统 毕业设计毕设源码毕业论文开题报告参考(1)功能概要

热门文章

  1. python创建docx文件
  2. PyTorch学习—10.nn中网络层的具体使用
  3. 用逻辑回归实现图像识别
  4. 机器学习入门——图解支持向量机
  5. 机器学习和数据挖掘的联系与区别
  6. 网站结构优化的基本方法
  7. iphone软件创富密码之 启动Xcode创建工程
  8. SPSS 17.0中文版常用功能与应用实例精讲
  9. 《linux核心应用命令速查》连载三:sa:报告、清理并维护进程统计文件
  10. 3.9 Spark 键值对RDD编程