【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;
}
相关推荐
小菜鸡桃蛋狗34 分钟前
C++——类和对象(下)
开发语言·c++
crescent_悦42 分钟前
C++:Highest Price in Supply Chain
开发语言·c++
feng_you_ying_li44 分钟前
底层实现map和set的第一步,AVL树的学习
c++
垫脚摸太阳1 小时前
第 36 场 蓝桥·算法挑战赛·百校联赛---赛后复盘
数据结构·c++·算法
Aaswk1 小时前
刷题笔记(回溯算法)
数据结构·c++·笔记·算法·leetcode·深度优先·剪枝
zhooyu1 小时前
GLM中lerp实现线性插值
c++·opengl
workflower1 小时前
设计模式的分类
设计模式·集成测试·软件工程·软件构建·软件需求·结对编程
我不是懒洋洋1 小时前
预处理详解
c语言·开发语言·c++·windows·microsoft·青少年编程·visual studio
计算机安禾2 小时前
【数据结构与算法】第14篇:队列(一):循环队列(顺序存储
c语言·开发语言·数据结构·c++·算法·visual studio
IT从业者张某某2 小时前
基于EGE19.01完成恐龙跳跃游戏-V00-C++使用EGE19.01这个轮子
c++·游戏