说明:
1.这个队列模板逻辑上以循环队列,物理上以元素数组为基础封装
2.队列只能在头出元素,在尾进元素;当头等于尾时认为队列空,当头的下一位置为尾时认为队列满
#ifndef SUNXYQUEUE_H
#define SUNXYQUEUE_H
const int QDEFAULT=200;
template<class T>
class SunxyQueue
{
public:
SunxyQueue(int s);
virtual ~SunxyQueue();
T Front();
T Back();
bool In(T entry);
T Out();
int ElemNum();
bool isEmpty();
bool isFull();
virtual void Display();
private:
int head,end,size;
T* array;
};
template<class T>
SunxyQueue<T>::SunxyQueue(int s=QDEFAULT)
{
head=end=0;
if(s<=QDEFAULT&&s>0)
size=s+1;
else
size=QDEFAULT+1;
array=new T[size];
}
template<class T>
SunxyQueue<T>::~SunxyQueue()
{
head=end=0;size=0;
delete[] array;
}
template<class T>
T SunxyQueue<T>::Front()
{
T p;
if(!this->isEmpty())
p=array[end];
return p;
}
template<class T>
T SunxyQueue<T>::Back()
{
T p;
if(!this->isEmpty())
p=(head==0)?array[size-1]:array[head-1];
return p;
}
template<class T>
bool SunxyQueue<T>::In(T entry)
{
bool sign;
if(!this->isFull())
{
array[head]=entry;
head++;
head=(head<size)?head:(head-size);
sign=true;
}
else
sign=false;
return sign;
}
template<class T>
T SunxyQueue<T>::Out()
{
T p;
if(!this->isEmpty())
{
p=array[end];
end++;
end=(end<size)?end:(end-size);
}
return p;
}
template<class T>
int SunxyQueue<T>::ElemNum()
{
int s;
if(!this->isEmpty())
{
if(this->isFull())
s=size-1;
else
s=(head>end)?(head-end):(size-end+head);
}
else
s=0;
return s;
}
template<class T>
bool SunxyQueue<T>::isEmpty()
{
if(head==end)
return true;
else
return false;
}
template<class T>
bool SunxyQueue<T>::isFull()
{
if((((head+1)<size)?(head+1):(head+1-size))==end)
return true;
else
return false;
}
template<class T>
void SunxyQueue<T>::Display()
{
//add codes here
//
}
#endif
本文地址:http://com.8s8s.com/it/it21901.htm