#include<iostream>
using namespace std;
const int maxqueue=10;
template <class T>
class ArrayQueue
{
public:
ArrayQueue();
bool empty() const;
bool full() const;
int size() const;
void append(T &item);
void serve();
T& retrieve();
void clear();
private:
int count;
int front,rear;
T array[maxqueue];
};
template <class T>
ArrayQueue<T>::ArrayQueue()
/*Pre: None
Post: The Queue is initialized to be empty.*/
{
count=0;
front=0;
rear=maxqueue-1;
}
template <class T>
bool ArrayQueue<T>::empty() const
/*Pre: None
Post: Return true if the Queue is empty,otherwise return false.*/
{
return count==0;
}
template <class T>
bool ArrayQueue<T>::full() const
/*Pre:None
Post:If the queue is full,true is returned.Otherwise,false is returned*/
{
return count==10;
}
template <class T>
int ArrayQueue<T>::size() const
/*Pre:None
Post:Return the number of entries in the queue.*/
{
return count;
}
template <class T>
void ArrayQueue<T>::append(T &item)
/*Pre: None
Post: Item is added to the rear of the Queue if it is not full,otherwise the queue is left unchanged.*/
{
if(count<maxqueue)
{
count++;
rear=((rear+1)==maxqueue)?0:(rear+1);
array[rear]=item;
}
}
template <class T>
void ArrayQueue<T>::serve()
/*Pre: None
Post: The front of the Queue is removed if the queue is not empty.*/
{
if(count>0)
{
count--;
front=((front+1)==maxqueue)?0:(front+1);
}
}
template <class T>
T& ArrayQueue<T>::retrieve()
/*Pre: None
Post: The front of the Queue is returnd if the queue is not empty.*/
{
if(count>0)
return array[front];
}
template <class T>
void ArrayQueue<T>::clear()
/*Pre: None
Post: The Queue is reset to be empty.*/
{
count=0;
front=0;
rear=maxqueue-1;
}
还没有评论,来说两句吧...