鏈隊列

#ifndef _LINKQUEUE_H_
#define _LINKQUEUE_H_

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

enum StatusCode{UNDER_FLOW,SUCCESS};
template<class ElemType>
class LinkQueue
{
	protected:
		Node<ElemType> *rear,*front;

		void Init();
	public:
		LinkQueue();
		virtual ~LinkQueue();

		int Length()const;
		bool Empty()const;
		void Clear();
			
		StatusCode InQueue(const ElemType &e);
		StatusCode OutQueue(ElemType &e);
		StatusCode GetQueue(ElemType &e)const;

		LinkQueue(const LinkQueue<ElemType> &copy);
		LinkQueue<ElemType>& operator = (const LinkQueue &copy);
};


template<class ElemType>
LinkQueue<ElemType>::LinkQueue()
{
	Init();
}

template<class ElemType>
LinkQueue<ElemType>::~LinkQueue()
{
//	cout<<"~LinkQueue"<<endl;
	Clear();
	delete front;
}

template<class ElemType>
void LinkQueue<ElemType>::Clear()
{
//	cout<<"Clear()"<<endl;
	ElemType elem;
	while(!Empty())
	{
	//	cout<<"while"<<endl;
		OutQueue(elem);
	//	cout<<elem<<endl;
	}
		
}

template<class ElemType>
bool LinkQueue<ElemType>::Empty()const
{
	return rear==front;
}

template<class ElemType>
void LinkQueue<ElemType>::Init()
{
	front=NULL;
	rear=NULL;
	front=new Node<ElemType>();
	rear=front;
}

template<class ElemType>
int LinkQueue<ElemType>::Length()const
{
	int count=0;
	for(Node<ElemType> *ptr=front->next;ptr!=NULL;ptr=ptr->next)
		count++;
	return count;
}

template<class ElemType>
StatusCode LinkQueue<ElemType>::InQueue(const ElemType &e)
{
	//cout<<"1"<<endl;
	Node<ElemType> *ptr=new Node<ElemType>(e,NULL);
	rear->next=ptr;
	rear=ptr;
	return SUCCESS;
}

template<class ElemType>
StatusCode LinkQueue<ElemType>::OutQueue(ElemType &e)
{
	//cout<<"1111"<<endl;
	if(Empty())
		return UNDER_FLOW;
	else{
		Node<ElemType> *ptr=front->next;
		e=ptr->data;
		front->next=ptr->next;
		if(ptr==rear)
			rear=front;
		delete ptr;
		return SUCCESS;
	}
}

template<class ElemType>
StatusCode LinkQueue<ElemType>::GetQueue(ElemType &e)const
{
	if(Empty())
		return UNDER_FLOW;
	else
	{
		e=(front->next)->data;
		return SUCCESS;
	}
}

template<class ElemType>
LinkQueue<ElemType>::LinkQueue(const LinkQueue<ElemType> &copy)
{
	ElemType elem;
	Init();
	for(int i=0;i<copy.Length();i++)
	{
		copy.OutQueue(elem);
		InQueue(elem);
		copy.InQueue(elem);
	}
}

template<class ElemType>
LinkQueue<ElemType>& LinkQueue<ElemType>::operator =(const LinkQueue<ElemType> &copy)
{
	if(&copy!=this)
	{
		Clear();
		ElemType elem;
		Init();
		for(int i=0;i<copy.Length();i++)
		{
			copy.OutQueue(elem);
			InQueue(elem);
			copy.InQueue(elem);
		n}
	}
	return this;

		
}
#endif



#ifndef _NODE_H_
#define _NODE_H_
#include <iostream>
using namespace std;

template<class ElemType>
struct Node
{
	ElemType data;
	Node<ElemType> *next;
	
	Node();
	Node(ElemType elem,Node<ElemType> *link=NULL);
};


template<class ElemType>
Node<ElemType>::Node()
{
	next=NULL;
}
template<class ElemType>
Node<ElemType>::Node(ElemType elem,Node<ElemType> *link)
{
	data=elem;
	next=link;
}

#endif


#include "Node.h"
#include "LinkQueue.h"
#include <iostream>
using namespace std;
 
void main()
{
	int elem;
	LinkQueue<int> a;
	a.InQueue(5);
	a.OutQueue(elem);
	cout<<elem<<endl;
}


發佈了445 篇原創文章 · 獲贊 27 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章