#include<iostream.h>
enum ResultCode{Underflow,Overflow};
template<class T>
class Queue
{
public:
virtual void Append(const T& x)=0;
virtual void Serve()=0;
virtual bool IsEmpty() const=0;
virtual bool IsFull() const=0;
};
template<class T>
class SeqQueue:public Queue<T>
{
public:
SeqQueue(){}
SeqQueue(int msize);
~SeqQueue(){delete []q;}
bool IsEmpty()const {return front==rear;}
bool IsFull()const
{
return (rear+1)%maxSize==front;
}
void Clear(){front=rear=0;}
void Append(const T&x);
void Serve();
T Front()const;
private:
T *q;
int maxSize;
int front,rear;
void Output(ostream& out)const;
friend ostream& operator<<(ostream& out,const SeqQueue<T>& q);
};
template<class T>
SeqQueue<T>::SeqQueue(int mSize)
{
maxSize=mSize+1;
q=new T[maxSize];
front=rear=0;
}
template<class T>
void SeqQueue<T>::Append(const T& x)
{
if(IsFull)throw Overflow;
q[rear=(rear+1)%maxSize]=x;
}
template<class T>
void SeqQueue<T>::Serve()
{
if(IsEmpty)throw Underfloe;
front=(front+1)%maxSize;
}
template<class T>
T SeqQueue<T>::Front()const
{
if(IsEmpty())throw Underflow;
return q[front+1];
}
template <class T>
SeqQueue<T>::Output(ostream& out)const
{
while(!(front+1)%maxSize==rear)
{
out<<q[front+1];
front++;
}
}
void main()
{
try
{
SeqQueue<int> h1(10);
h1.Append(10);
h1.Append(20);
h1.Serve();
h1.Front();
h1.Append(53);
h1.Append(56);
h1.Output(cout);
}
catch(enum ResultCode err)
{
switch(err)
{
case Overflow:cout<<"wrong1"<<end;break;
case Underflow:cout<<"wrong2"<<endl;break;
}
}
}