C++队列的实现

List.h文件

#include<iostream>
using namespace std;

template<class T>
struct Nodetype{
	Nodetype(const T& data = T())
	: _pNext(NULL)
	, _pPre(NULL)
	, _data(data)
	{}

	Nodetype<T>* _pNext;
	Nodetype<T>* _pPre;
	T _data;
};

template<class T>
class ListIterator{
	typedef Nodetype<T>* pNode;
	typedef ListIterator<T> Self;
public:
	ListIterator()
		:_pCur(NULL)
	{}
	ListIterator(pNode pCur)
		: _pCur(pCur)
	{}
	ListIterator(const Self& s)
		: _pCur(s._pCur)
	{}
	Self& operator++()
	{
		_pCur = _pCur->_pNext;
		return *this;
	}
	Self operator++(int)
	{
		Self temp(*this);
		_pCur = _pCur->_pNext;
		return temp;
	}
	Self& operator--()
	{
		_pCur = _pCur->_pPre;
		return *this;
	}
	Self operator--(int)
	{
		Self temp(*this);
		_pCur = _pCur->_pPre;
		return *this;
	}
	bool operator!=(const Self& s)
	{
		return _pCur != s._pCur;
	}
	bool operator==(const Self& s)
	{
		return _pCur == s._pCur;
	}
	T& operator*()
	{
		return _pCur->_data;
	}
	T* operator->()
	{
		return *(_pCur->_data);
	}
private:
	pNode _pCur; 
};


template<class T>
class List{
	typedef Nodetype<T> Node;
	typedef Node* pNode;
public:
	typedef ListIterator<T> Iterator;
public:
	List()
	{
		CreatListHead();
	}

	List(T* arr, size_t size)
	{
		//创建头节点
		CreatListHead();
		for (size_t i = 0; i < size; ++i)
		{
			PushBack(arr[i]);
		}
	}

	List(const List<T>& l);
	List<T>& operator=(const List<T>& l);
	~List();
	void PushBack(const T& data);
	void PopBack(const T& data);
	void PushFront(const T& data);
	void PopFront();
	void Clear();
	void Print();
	//pNode Insert(pNode pos, const T& data);
	pNode Insert(pNode pos, const T& data){
		pNode pNewNode = new Node(data);
		pos->_pPre->_pNext = pNewNode;
		pNewNode->_pPre = pos->_pPre;
		pos->_pPre = pNewNode;
		pNewNode->_pNext = pos;
		return pNewNode;
	}
	//pNode Erase(pNode pos);
	pNode Erase(pNode pos){
		pNode pCur = pos->_pNext;
		pos->_pPre->_pNext = pos->_pNext;
		pos->_pNext->_pPre = pos->_pPre;
		delete pos;
		return pCur;
	}

	size_t size()const;

	bool Empty()const
	{
		if (pHead->_pNext == pHead)
			return true;
		return false;
	}

	Iterator Begin()
	{
		return Iterator(pHead->_pNext);
	}
	Iterator End()
	{
		return Iterator(pHead);
	}
	T& Front()
	{
		return pHead->_pNext->_data;
	}
private:
	void CreatListHead()
	{
		pHead= new Node;
		pHead->_pNext = pHead;
		pHead->_pPre = pHead;
	}

	pNode pHead;
};

template<class T>
void List<T>::PushBack(const T& data){
	pNode pTail = pHead->_pPre;
	pNode pNewNode = new Node(data);
	pTail->_pNext = pNewNode;
	pNewNode->_pPre = pTail;
	pNewNode->_pNext = pHead;
	pHead->_pPre = pNewNode;
}

template<class T>
void List<T>::PopBack(const T& data){
	if (Empty())
		return;
	pNode pProTail = pHead->_pPre->_pPre;
	delete pProTail->_pNext;
	pProTail->_pNext = pHead;
	pHead->_pPre = pProTail;
}

template<class T>
void List<T>::PushFront(const T& data){
	pNode pNewNode = new Node(data);
	pNewNode->_pNext = pHead->_pNext;
	pHead->_pNext->_pPre = pNewNode;
	pHead->_pNext = pNewNode;
	pNewNode->_pPre = pHead;
}

template<class T>
void List<T>::PopFront(){
	if (Empty())
		return;
	pNode pTail = pHead->_pNext;
	pHead->_pNext = pTail->_pNext;
	pTail->_pNext->_pPre = pHead;
	delete pTail;
}
template<class T>
size_t List<T>::size()const{
	pNode pCur = pHead->_pNext;
	size_t count = 0;
	while (pCur != pHead){
		count++;
		pCur = pCur->_pNext;
	}
	return count;
}

template<class T>
void List<T>::Clear(){
	pNode pDel = pHead->_pNext;
	while (!Empty()){
		pDel = pHead->_pNext;
		pHead->_pNext = pDel->_pNext;
		delete pDel;
	}
}

template<class T>
List<T>::~List(){
	Clear();
	delete pHead;
	pHead = NULL;
}

template<class T>
List<T>::List(const List<T>& l){
	pHead = new Node();
	pHead->_pNext = pHead;
	pHead->_pPre = pHead;

	pNode pCur = (l.pHead)->_pNext;
	while (pCur != l.pHead)
	{
		this->PushBack(pCur->_data);
		pCur = pCur->_pNext;
	}
}

template<class T>
List<T>& List<T>::operator=(const List<T>& l){
	if (this != &l)
	{
		this->Clear();
		pNode pCur = (l.pHead)->_pNext;
		while (pCur != l.pHead)
		{
			this->PushBack(pCur->_data);
			pCur = pCur->_pNext;
		}
	}
	return *this;
}

template<class T>
void List<T>::Print()
{
	pNode pCur = pHead->_pNext;
	while (pCur != pHead)
	{
		cout << pCur->_data << " ";
		pCur = pCur->_pNext;
	}
	cout << endl;
}

Queue.h文件

#include<iostream>
using namespace std;
#include"List.h"

template<class T, template<class> class Container = List>
class Queue{
public:
	Queue()
	{}
	void Push(const T& data)
	{
		_con.PushBack(data);
	}
	void Pop()
	{
		_con.PopFront();
	}
	void print()
	{
		_con.Print();
	}
	bool Empty()const
	{
		return _con.Empty();
	}
	size_t size()const
	{
		return _con.size();
	}
	T& Front()
	{
		return _con.Front();
	}
private:
	Container<T> _con;
};

test.c文件 

#include<iostream>
using namespace std;
#include"Queue.h"


void test(){
	Queue<int> q;
	q.Push(1);
	q.print();
	cout << q.size() << endl;
	q.Push(2);
	q.print();
	cout << q.size() << endl;
	q.Push(3);
	q.print();
	cout << q.size() << endl;
	cout << q.Front() << endl;
	q.Push(4);
	q.print();
	cout << q.size() << endl;
	q.Push(5);
	q.print();
	cout << q.size() << endl;
	q.Pop();
	q.print();
	cout << q.size() << endl;
	q.Pop();
	q.print();
	cout << q.size() << endl;
	q.Pop();
	q.print();
	cout << q.size() << endl;
	q.Pop();
	q.print();
	cout << q.size() << endl;
	q.Pop();
	q.print();
	cout << q.size() << endl;
	q.Empty();
	q.print();
	cout << q.size() << endl;
}

int main(){
	test();
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值