本文已参与「新人创作礼」活动,一起开启掘金创作之路。
游戏开发之使用类封装双链表数据结构及双链表迭代器实现初版(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;
}
版本声明:本文为CSDN博主[ufgnix0802]的原创文章。
原文链接:(blog.csdn.net/qq135595696…)