#include<assert.h>
#include<iostream.h>
#include<stdlib.h>
#include"Queue.h"
template<class T>
class SeqQueue
{
public:
SeqQueue(int sz=10);
~SeqQueue(){delete[]elements;}
bool EnQueue(const T& x);
bool DeQueue(T& x);
bool getFront(T& x);
void makeEmpty(){front=rear=0;}
bool IsEmpty()const{return(front==rear)?true:false;}
bool IsFull()const{return((rear+1)%maxSize==front)?true:false;}
friend ostream& operator<<(ostream& os,SeqQueue<T>& Q); //输出重载
protected:
int rear,front; //队尾指针与对头指针
T *elements;
int maxSize; //队列最大可容纳个数
};
template<class T>
SeqQueue<T>::SeqQueue(int sz):front(0),rear(0),maxSize(sz)
{
elements=new T[maxSize];
assert(elements!=NULL);
};
template<class T>
bool SeqQueue<T>::EnQueue(const T& x)
{
if(IsFull()==true) return false; //队列满,插入失败
elements[rear]=x; //按照队尾指针指示插入位置
rear=(rear+1)%maxSize; //队尾指针+1
return true;
};
template<class T>
bool SeqQueue<T>::DeQueue(T& x)
{
if(IsEmpty()==true) return false;
x=elements[front];
front=(front+1)%maxSize;
return true;
}
template<class T>
bool SeqQueue<T>::getFront(T& x)
{
if(IsEmpty()==true) return false;
x=elements[front];
return true;
};
template<class T>
ostream& operator<<(ostream& os,SeqQueue<T>& Q)
{
os<<"front="<<Q.front<<",rear="<<Q.rear<<endl;
for(int i=Q.front;i!=Q.rear;i=(i+1)%maxSize)
os<<i<<":"<<Q.elements[i]<<endl;
return os;
}