顺序队列

SeqQueue.h

template<typename Type>class SeqQueue{
private:
	int m_nrear;
	int m_nfront;
	int m_ncount;
	int m_nMaxSize;
	Type *m_pelements;
public:
	SeqQueue(int sz):m_nrear(0),m_nfront(0),m_nMaxSize(sz),m_ncount(0)
	{
		m_pelements = new Type[sz];
		if(m_pelements == NULL)
		{
			cout<<"Application Error!"<<endl;
			exit(1);
		}
	}
	~SeqQueue(){delete[] m_pelements;}
	void MakeEmpty();
	bool IsEmpty();
	bool IsFull();
	bool Append(const Type item);
	Type Delete();
	Type Get();
	void Print();
};

SeqQueue.cpp

template<typename Type> void SeqQueue<Type>::MakeEmpty(){
	m_ncount = 0;
	m_nfront = 0;
	m_nrear = 0;
}

template<typename Type> bool SeqQueue<Type>::IsEmpty(){
	if(m_ncount == m_nfront && m_ncount == 0)
		return 1;
	return 0;
}

template<typename Type> bool SeqQueue<Type>::IsFull(){
	if(m_ncount == m_nMaxSize)
		return 1;
	return 0;
}

template<typename Type> bool SeqQueue<Type>::Append(const Type item){
	if(IsFull())
	{
		cout<<"The queue is full!"<<endl;
		return 0;
	}
	m_pelements[m_nrear] = item;
	m_nrear = (m_nrear + 1) % m_nMaxSize;
	m_ncount++;
	return 1;
}

template<typename Type> Type SeqQueue<Type>::Delete(){
	if(IsEmpty())
	{
		cout<<"The queue is empty!"<<endl;
		return 0;
	}
	Type temp = m_pelements[m_nfront];
	m_nfront = (m_nfront + 1) % m_nMaxSize;
	m_ncount--;
	return temp;
}

template<typename Type> Type SeqQueue<Type>::Get(){
	if(IsEmpty())
	{
		cout<<"The queue is empty!"<<endl;
		return 0;
	}
	return m_pelements[m_nfront];
}

template<typename Type> void SeqQueue<Type>::Print(){
	for(int i = m_nfront,j = 0; j < m_ncount; i = (i+1)%m_nMaxSize,j++)
		cout<<m_pelements[i]<<' ';
	cout<<endl;
}

Test.cpp

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

int main(){
	SeqQueue<int> queue(10);
	int init[10]={1,6,9,0,2,5,8,3,7,4};
	for(int i=0;i<5;i++){
		queue.Append(init[i]);
	}
	queue.Print();

	cout<<queue.Delete()<<endl;
	queue.Print();

	for(int i=5;i<10;i++){
		queue.Append(init[i]);
	}
	queue.Print();

	cout<<queue.Get()<<endl;

	queue.MakeEmpty();
	queue.Print();

	queue.Append(1);
	queue.Print();

	return 0;
}

你可能感兴趣的:(顺序队列)