【27】c++设计模式——>迭代器模式(遍历双向链表)(2)

cpp 复制代码
//实现双向链表
#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;
}
相关推荐
是阿建吖!3 分钟前
【优选算法】二分查找
c++·算法
Ajiang28247353042 小时前
对于C++中stack和queue的认识以及priority_queue的模拟实现
开发语言·c++
哪 吒5 小时前
最简单的设计模式,抽象工厂模式,是否属于过度设计?
设计模式·抽象工厂模式
Theodore_10225 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
‘’林花谢了春红‘’7 小时前
C++ list (链表)容器
c++·链表·list
转世成为计算机大神8 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
机器视觉知识推荐、就业指导8 小时前
C++设计模式:建造者模式(Builder) 房屋建造案例
c++
小乖兽技术9 小时前
23种设计模式速记法
设计模式
Yang.9910 小时前
基于Windows系统用C++做一个点名工具
c++·windows·sql·visual studio code·sqlite3