c++第一次实现双向链表附迭代器

15 篇文章 0 订阅
14 篇文章 0 订阅

双向链表,下一步就是类模板参数和迭代器实现一些简单算法!!

代码量等知识储备够了再优化

异常还理解不了

#ifndef LIST_H_
#define LIST_H_

#include<iostream>

using std::underflow_error;

namespace LIST
{
	//结点
	template<typename Ty>
	struct Node
	{
		inline Node() { this->front = nullptr, this->rear = nullptr; }
		//前驱
		Node<Ty>* front;
		//值
		Ty value;
		//后继
		Node<Ty>* rear;
	};
	template<typename Type>
	class List
	{
	public:
		//默认
		List();
		//n个type
		List(const int, const Type&);
		//copy
		List(const List<Type>&);
		//析构
		~List();
		//赋值
		void assign(const int, const Type&);
		void assign(const List<Type>&);
		//尾部部操作
		void push_back(const Type&);
		void pop_back();
		//头部操作
		void push_fornt(const Type&);
		void pop_fornt();
		//赋值
		List<Type>& operator=(const List<Type>&);
		//清空
		void rease();
		//是否为空
		const bool empty();
		//占用
		const int size();
		//迭代器
		class Iterator
		{
		public:
			Iterator()
			{
				this->it_begin = nullptr;
				this->it_end = nullptr;
				this->it_move = nullptr;
			}
			Iterator& operator++()
			{
				if (this->it_move == this->it_end->rear)
					throw underflow_error("无效的访问:链表已到底");
				this->it_move = this->it_move->rear;
				return *this;
			}
			Iterator& operator--()
			{
				if (this->it_move ==this->it_begin)
					throw underflow_error("无效的访问:链表已到头");
				this->it_move = this->it_move->front;
				return *this;
			}
			Type& operator*()
			{
				if (this->it_begin == this->it_end->front)
					throw underflow_error("链表为空,无法访问");
				return this->it_move->value;
			}
			Node<Type>* it_begin;
			Node<Type>* it_move;
			Node<Type>* it_end;
		};
		//返回迭代器
		const Iterator begin()const
		{
			List::Iterator it{};
			it.it_begin = this->m_forward->rear;
			it.it_end = this->m_back;
			it.it_move = it.it_begin;
			return it;
		}
		const Iterator end()const
		{
			List::Iterator it{};
			it.it_begin = this->m_forward->rear;
			it.it_end = this->m_back;
			it.it_move = it.it_end;
			return it;
		}
	private:
		//头结点指针
		Node<Type>* m_forward;
		//尾节点指针
		Node<Type>* m_back;
		//自由前节点指针
		Node<Type>* m_freed_forward;
		Node<Type>* m_freed_back;
		//容器占用
		int m_size;
		//辅助函数
		//头尾结点指针归位
		inline void launch(); 
	};
	//默认构造
	template<typename Type>
	LIST::List<Type>::List() { this->launch(); }
	//n个type
	template<typename Type>
	LIST::List<Type>::List(const int n, const Type&Ty)
	{
		this->launch();
		while (n != this->m_size)
			this->push_back(Ty);
	}
	//copy
	template<typename Type>
	LIST::List<Type>::List(const List<Type>&li)
	{
		this->launch();
		List<Type>::Iterator it = li.begin();
		for (int i = 0; i < li.m_size; ++i)
		{
			this->m_back->value =*it;
			this->m_back = new Node<Type>;
			this->m_freed_back->rear = this->m_back;
			this->m_back->front = this->m_freed_back;
			this->m_freed_back = this->m_back;
			++this->m_size;
			++it;
		}
	}
	//析构
	template<typename Type>
	LIST::List<Type>::~List()
	{
		while(this->m_size!=0)
			this->pop_back();
		delete this->m_forward;
		delete this->m_back;
	}
	//类赋值
	template<typename Type>
	void LIST::List<Type>::assign(const int n, const Type& Ty)
	{
		while (this->m_size != 0)
			this->pop_back();
		while (this->m_size != n)
			this->push_back(Ty);
	}
	template<typename Type>
	void LIST::List<Type>::assign(const List<Type>&li)
	{
		while(this->m_size!=0)
			this->pop_back();
		List<Type>::Iterator it = li.begin();
		for (int i = 0; i < li.m_size; ++i)
		{
			this->m_back->value = *it;
			this->m_back = new Node<Type>;
			this->m_freed_back->rear = this->m_back;
			this->m_back->front = this->m_freed_back;
			this->m_freed_back = this->m_back;
			++this->m_size;
			++it;
		}
	}
	template<typename Type>
	List<Type>& LIST::List<Type>::operator=(const List<Type>&li)
	{
		while(this->m_size!=0)
			this->pop_back();
		List<Type>::Iterator it = li.begin();
		for (int i = 0; i < li.m_size; ++i)
		{
			this->m_back->value = *it;
			this->m_back = new Node<Type>;
			this->m_freed_back->rear = this->m_back;
			this->m_back->front = this->m_freed_back;
			this->m_freed_back = this->m_back;
			++this->m_size;
			++it;
		}
		return *this;
	}
	//尾插
	template<typename Type>
	void LIST::List<Type>::push_back(const Type& Ty)
	{
		//存值
		this->m_back->value = Ty;
		//创建新的尾节点
		this->m_back = new Node<Type>;
		//链接
		this->m_freed_back->rear = this->m_back;
		this->m_back->front = this->m_freed_back;
		//更新占用并归位
		this->m_freed_back = this->m_back;
		++this->m_size;
	}
	//尾删
	template<typename Type>
	void LIST::List<Type>::pop_back()
	{
			if (this->empty())
				throw underflow_error("容器为空,删除失败");
		//改变尾节点
		this->m_back = this->m_back->front;
		//清除
		delete this->m_freed_back;
		//更新占用,归位
		this->m_freed_back = this->m_back;
		--this->m_size;
	}
	//头插
	template<typename Type>
	void LIST::List<Type>::push_fornt(const Type& Ty)
	{
		//存值
		this->m_forward->value = Ty;
		//创建新的头结点
		this->m_forward = new Node<Type>;
		//链接
		this->m_freed_forward->front = this->m_forward;
		this->m_forward->rear = this->m_freed_forward;
		//更新占用并归位
		this->m_freed_forward = this->m_forward;
		++this->m_size;
	}
	//头删
	template<typename Type>
	void LIST::List<Type>::pop_fornt()
	{
		if (this->empty())
			throw underflow_error("容器为空,删除失败");
		//改变头结点
		this->m_forward = this->m_forward->rear;
		//清除
		delete this->m_freed_forward;
		//更新占用并归位
		this->m_freed_forward = this->m_forward;
		--this->m_size;
	}
	//辅助,头尾结点归位
	template<typename Type>
	void LIST::List<Type>::launch()
	{
		//初始化指针和占用
		this->m_forward = new Node<Type>;
		this->m_back = new Node<Type>;
		this->m_freed_forward = this->m_forward;
		this->m_freed_back = this->m_back;
		this->m_size = 0;
		//链接
		this->m_forward->rear = this->m_back;
		this->m_back->front = this->m_forward;
	}
	//杂项函数
	template<typename Type>
	const bool LIST::List<Type>::empty()
	{
		if (this->m_size)
			return false;
		return true;
	}
	template<typename Type>
	const int LIST::List<Type>::size()
	{
		return this->m_size;
	}
	template<typename Type>
	void LIST::List<Type>::rease()
	{
		while(this->m_size!=0)
			this->pop_back();
	}
}
#endif // !LIST_H_

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值