template<typename Type> class SeqQueue
{
private:
int rear; //指向队尾
int front; //指向队头
int count; //队列当前的元素个数
int MaxSize; //队列的最大元素个数
Type *pelements; //指向队列元素的指针
public:
SeqQueue(int sz): rear(0),front(0),count(0),MaxSize(sz)
{
pelements = new Type[sz];
if(pelements == NULL)
{
cout<<"Application error"<<endl;
exit(1);
}
}
~SeqQueue()
{
delete [] pelements;
}
public:
void MakeEmpty();
bool IsEmpty();
bool IsFull();
bool Append(const Type item);
Type Delete();
Type Get();
void Print();
};
template<typename Type>
void SeqQueue<Type>::MakeEmpty()
{
this->front = 0;
this->rear = 0;
this->count = 0;
}
template<typename Type>
bool SeqQueue<Type>::IsEmpty()
{
return count == 0;
}
template<typename Type>
bool SeqQueue<Type>::IsFull()
{
return count == MaxSize;
}
template<typename Type>
bool SeqQueue<Type>::Append(const Type item)
{
if(count == MaxSize)
{
cout<<"the queue has been full"<<endl;
return false;
}
pelements[rear] = item;
rear = (rear+1) % MaxSize;
count++;
return true;
}
template<typename Type>
Type SeqQueue<Type>::Delete()
{
if(count == 0)
{
cout<<"the queue has been empty"<<endl;
exit(1);
}
Type item = pelements[front];
front = (front+1) % MaxSize;
count--;
return item;
}
template<typename Type>
Type SeqQueue<Type>::Get()
{
if( count == 0 )
{
cout<<"the queue has been empty"<<endl;
exit(1);
}
return pelements[front];
}
template<typename Type>
void SeqQueue<Type>::Print()
{
cout<<"font";
for(int i=0; i<count; ++i)
{
cout<<"-->"<<pelements[ (front+i) % MaxSize ];
}
cout<<"-->rear"<<endl;
}
c++实现数据结构七 顺序循环队列
最新推荐文章于 2022-05-14 14:24:42 发布