//实现双向链表
#pragma once
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Iterator;
class ForwardIterator;
class ReverseIterator;
//链表的最小组成部分是一个节点,先实现一个节点
struct Node //c++中struct和class没有区别,只不过class默认是private,struct默认是public
{
Node(int n) :name(n) {} //构造函数
int name; //数据
Node* next = nullptr; //后继
Node* prev = nullptr; //前驱
};
//定义双向链表
class MyList
{
public:
//定义成内联函数(内存替换,不能太复杂,空间换时间),获取链表节点个数
inline int getCount()
{
return m_count;
}
//获取链表头节点
inline Node* head()
{
return m_head;
}
//获取链表尾节点
inline Node* tail()
{
return m_tail;
}
//定义插入节点的操作,头部插入,中间插入,尾部插入
Node* insert(Node* node,int name);
Node* pushFront(int name);
Node* pushBack(int name);
Iterator* getIterator(bool isReverse);
private:
Node* m_head = nullptr; //定义一个指向头结点的指针
Node* m_tail = nullptr; //定义一个指向尾结点的指针
int m_count = 0;
};
Node* MyList::insert(Node* node, int name)
{
Node* newNode = nullptr;
//node是链表中的哪个节点,如果是头结点
if (node == m_head)
{
newNode = pushFront(name);
}
else if (node == m_tail)
{
newNode = pushBack(name);
}
else
{
newNode = new Node(name);
newNode->next = node;
newNode->prev = node->prev;
node->prev->next = newNode;
node->prev = newNode;
m_count++;
}
return newNode;
}
Node* MyList::pushFront(int name)
{
Node* newNode = new Node(name);
if (m_count == 0)
{
m_head = m_tail = newNode;
}
else
{
newNode->next = m_head;
m_head->prev = newNode;
m_head = newNode;
}
m_count++;
return newNode;
}
Node* MyList::pushBack(int name)
{
Node* newNode = new Node(name);
if (m_count == 0)
{
m_head = m_tail = newNode;
}
else
{
newNode->prev = m_tail;
m_tail->next = newNode;
m_tail = newNode;
}
m_count++;
return newNode;
}
//定义抽象的迭代器类
class Iterator
{
public:
Iterator(MyList* list) : m_list(list) {} //构造函数,保存好要遍历的链表
Node* current()
{
return m_current;
}
virtual Node* first() = 0;
virtual Node* next() = 0;
virtual bool isDone() = 0;
protected:
MyList* m_list = nullptr;
Node* m_current = nullptr;
};
//定义一个正向遍历的链表
class ForwardIterator:public Iterator
{
public:
using Iterator::Iterator;
Node* first() override
{
m_current = m_list->head();
return m_current;
}
Node* next() override
{
m_current = m_current->next;
return m_current;
}
bool isDone() override
{
return m_current == nullptr;
}
};
//定义一个反向遍历的链表
class ReverseIterator :public Iterator
{
public:
using Iterator::Iterator;
Node* first() override
{
m_current = m_list->tail();
return m_current;
}
Node* next() override
{
m_current = m_current->prev;
return m_current;
}
bool isDone() override
{
return m_current == nullptr;
}
};
Iterator* MyList::getIterator(bool isReverse)
{
Iterator* it = nullptr;
if (isReverse)
{
it = new ReverseIterator(this);
}
else
{
it = new ForwardIterator(this);
}
return it;
}
int main()
{
vector<int> nameList = {0,1,2,3,4,5,6,7,8,9};
MyList ls;
for (auto& itr : nameList)
{
ls.pushBack(itr);
}
//正向遍历
Iterator* it = ls.getIterator(true);
for (auto begin = it->first(); !it->isDone(); it->next())
{
cout << it->current()->name << endl;
}
cout << "==================================================" << endl;
Iterator* ir = ls.getIterator(false);
for (auto begin = ir->first(); !ir->isDone(); ir->next())
{
cout << ir->current()->name << endl;
}
return 0;
}