【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;
}
相关推荐
lsswear5 小时前
PHP 设计模式
设计模式·php
tjl521314_2110 小时前
04C++ 名称空间(Namespace)
开发语言·c++
ximu_polaris10 小时前
设计模式(C++)-行为型模式-备忘录模式
c++·设计模式·备忘录模式
05候补工程师11 小时前
[实战复盘] 拒绝 AI 屎山!我从设计模式中学到的“调教”AI 新范式
人工智能·python·设计模式·ai·ai编程
tankeven14 小时前
C++ 智能指针
c++
sg_knight16 小时前
Python 设计模式:迭代器模式——用优雅的方式遍历一切
python·设计模式·迭代器模式
handler0117 小时前
【算法模板】最小生成树:稠密图选 Prim,稀疏图选 Kruskal
c语言·数据结构·c++·算法
许长安17 小时前
RPC 异步调用基本使用方法:基于官方helloworld-async 示例
c++·经验分享·笔记·rpc
sparEE17 小时前
c++面向对象:对象的赋值
开发语言·c++
此生决int17 小时前
快速复习之数据结构篇——栈和队列
数据结构·c++