template<typename ElemType>
class SqQueue
{
protected:
int count;
int front,rear;
int maxSize;
ElemType *elem;
public:
SqQueue(){}
SqQueue(int size);
virtual ~SqQueue();
int Length() const;
bool Empty() const;
void Clear();
void Traverse(void (*visit)(const ElemType &))const;
bool OutQueue(ElemType &e);
bool GetHead(ElemType &e) const;
bool InQueue(const ElemType &e);
SqQueue(const SqQueue<ElemType> &copy);
SqQueue<ElemType> &operator =(const SqQueue<ElemType> &copy);
};
template<typename ElemType>
//构造函数
SqQueue<ElemType>::SqQueue(int size)
{
maxSize=size;
elem=new ElemType[maxSize];
rear=front=;
count=;
}
template<typename ElemType>
//虚虚构函数
SqQueue<ElemType>::~SqQueue()
{
delete []elem;
}
template<typename ElemType>
//SqQueue长度
int SqQueue<ElemType>::Length() const
{
return count;
}
template<typename ElemType>
//判断空队列
bool SqQueue<ElemType>::Empty() const
{
return count==;
}
template<typename ElemType>
//清空队列
void SqQueue<ElemType>:: Clear()
{
rear=front=;
count=;
}
template<typename ElemType>
//遍历队列
void SqQueue<ElemType>::Traverse(void(*visit)(const ElemType & ))const
{
for(int pos=front;pos!=rear;pos=(pos+)%maxSize)
(*visit)(elem[pos]); }
template<typename ElemType>
//队首出
bool SqQueue<ElemType>::OutQueue(ElemType &e)
{
if(!Empty())
{
e=elem[front];
front=(front+)%maxSize;
count--;
return true;
}
else return false;
}
template<typename ElemType>
//加入新队尾
bool SqQueue<ElemType>::InQueue(const ElemType &e)
{
if(count==maxSize)
return false;
else
{
elem[rear]=e;
rear=(rear+)%maxSize;
count++;
return true;
} }
template<typename ElemType>
//复制构造
SqQueue<ElemType>::SqQueue(const SqQueue<ElemType> &copy)
{
maxSize=copy.maxSize;
elem=new ElemType[maxSize];
front=copy.front;
rear=copy.rear;
count=copy.count;
for(int pos=front;pos!=rear;pos=(pos+)%maxSize)
elem[pos]=copy.elem[pos];
}
template<typename ElemType>
//重载=operator
SqQueue<ElemType> &SqQueue<ElemType>::operator=(const SqQueue<ElemType> &copy)
{
if(&copy!=this)
{
maxSize=copy.maxSize;
delete []elem;
elem=new SqQueue<ElemType>[maxSize];
count=copy.count;
front=copy.front;
rear=copy.rear;
for(int pos=front;pos!=rear;pos=(pos+)%maxSize)
elem[pos]=copy.elem[pos];
return *this;
} }
05-11 11:03