# 实验二 顺序队列

xiaoxiao2021-02-28  37

#include<iostream> using namespace std; const int QueueSize=100; class CirQueue { public: CirQueue() { front=rear=QueueSize-1; } ~CirQueue(){} void EnQueue(int x); int DeQueue(); int GetQueue(); int Empty() { int w; front==rear?w=1:w=0; return w; } 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]; } int main() { CirQueue C; cout<<"******************"<<endl; cout<<"      顺序队列"<<endl; cout<<"******************"<<endl; cout<<endl; cout<<"@队列的属性为："<<endl; if(C.Empty()) cout<<"队列非空"<<endl; else cout<<"队列为空"<<endl; cout<<endl; cout<<"@元素1和2执行入队操作"<<endl; C.EnQueue(1); C.EnQueue(2); cout<<endl; cout<<"@查看队头元素"<<endl; cout<<C.GetQueue()<<endl; cout<<endl; cout<<"@执行出队操作"<<endl; C.DeQueue(); cout<<endl; cout<<"@查看队头元素"<<endl; cout<<C.GetQueue()<<endl; return 0;

}