#include<iostream>
using namespace std;
const int QueueSize=100;
template<class T>
class CirQueue
{
public:
CirQueue();
~CirQueue(){}
void enqueue(T x);
T dequeue();
T getqueue();
int empty();
private:
T data[QueueSize];
int front,rear;
};
template<class T>
CirQueue<T>::CirQueue()
{
front=rear=QueueSize-1;
}
template<class T>
void CirQueue<T>::enqueue(T x)
{
if((rear+1)%QueueSize==front)throw"上溢";
rear=(rear+1)%QueueSize;
data[rear]=x;
}
template<class T>
T CirQueue<T>::dequeue()
{
if(rear==front)throw"下溢";
front=(front+1)%QueueSize;
return data[front];
}
template<class T>
int CirQueue<T>::empty()
{
if(front==rear)
return 1;
else return 0;
}
template<class T>
T CirQueue<T>::getqueue()
{
int i;
if(rear==front)throw"下溢";
i=(front+1)%QueueSize;
return data[i];
}
void main()
{
CirQueue<int>s;
if(s.empty())
cout<<"队列为空";
else cout<<"队列非空 ";
cout<<"1,2,3入队"<<endl;
s.enqueue(1);
s.enqueue(2);
s.enqueue(3);
cout<<"队头为"<<endl;
cout<<s.getqueue()<<endl;
cout<<"1出队"<<endl;
s.dequeue();
cout<<"队头为"<<endl;
cout<<s.getqueue()<<endl;
}
队列
最新推荐文章于 2023-12-25 09:40:40 发布