#include<iostream.h>
const int QueueSize=100;
class CirQueue
{
public:
CirQueue(){front=rear=QueueSize-1;}
~CirQueue(){}
void EnQueue(int x);
int DeQueue();
int GetQueue();
private:
int Data[QueueSize];
int front,rear;
};
void CirQueue::EnQueue(int x)
{
if((rear+1)%QueueSize==front)throw"上溢";
rear=(rear+1)%QueueSize;
Data[rear]=x;
}
int CirQueue::DeQueue()
{
if(rear==front) throw"下溢";
front=(front+1)%QueueSize;
return Data[front];
}
int CirQueue::GetQueue()
{
int i;
if(rear==front) throw"下溢";
i=(front+1)%QueueSize;
return Data[i];
}
void main()
{
CirQueue C;
cout<<"元素13和14执行入队操作:"<<endl;
try
{
C.EnQueue(13);
C.EnQueue(14);
}
catch(char* wrong)
{
cout<<wrong<<endl;
}
cout<<"查看队头元素:"<<endl;
cout<<C.GetQueue()<<endl;
cout<<"执行出队操作:"<<endl;
try
{
C.DeQueue();
}
catch (char* wrong)
{
cout<<wrong<<endl;
}
cout<<"查看队头元素:"<<endl;
cout<<C.GetQueue()<<endl;
}
实验二顺序队列
最新推荐文章于 2020-03-05 16:24:40 发布