昨天苏州oj爆掉了,就没发,今天补上,利用的是循环队列来做的
内容:
【问题描述】
请设计一个程序模拟队列,具备初始队列、入队、出队、输出队列数据的功能。
【输入】
输入由若干行组成,每行表示一种操作。每行由一个或两个整数组成,其中第一个整数代表操作的类型:1表示初始队列操作,2表示入队操作,3表示出队操作、4表示输出队列信息,第二个整数表示操作所涉及的数据。
初始队列操作:将队列置为空并重新设置队列容量。该行第二个整数设为队列的容量。
入队操作:将该行第二个整数入队,如队列中元素个数超过队列容量则输出“Full OV”。
出队操作:正常删除队首元素,如果队列中已无任何元素,则输出“Empty OV”。
输出操作:按从队首到队尾的顺序输出队内所有元素,空队列输出“Empty”。
【输出】
根据不同的操作,输出不同的操作信息。
初始队列操作无输出信息。入队、出队如有错则输出出错信息,否则无输出信息。输出操作按从队首到队尾的顺序输出队内所有元素(元素之间用一个空格隔开)。
【样例】
输入
1 2
3
2 78
2 88
2 99
3
4
3
4
2 99
4
输出
Empty OV
Full OV
88
Empty
99
请设计一个程序模拟队列,具备初始队列、入队、出队、输出队列数据的功能。
【输入】
输入由若干行组成,每行表示一种操作。每行由一个或两个整数组成,其中第一个整数代表操作的类型:1表示初始队列操作,2表示入队操作,3表示出队操作、4表示输出队列信息,第二个整数表示操作所涉及的数据。
初始队列操作:将队列置为空并重新设置队列容量。该行第二个整数设为队列的容量。
入队操作:将该行第二个整数入队,如队列中元素个数超过队列容量则输出“Full OV”。
出队操作:正常删除队首元素,如果队列中已无任何元素,则输出“Empty OV”。
输出操作:按从队首到队尾的顺序输出队内所有元素,空队列输出“Empty”。
【输出】
根据不同的操作,输出不同的操作信息。
初始队列操作无输出信息。入队、出队如有错则输出出错信息,否则无输出信息。输出操作按从队首到队尾的顺序输出队内所有元素(元素之间用一个空格隔开)。
【样例】
输入
1 2
3
2 78
2 88
2 99
3
4
3
4
2 99
4
输出
Empty OV
Full OV
88
Empty
99
以下是代码
#include<iostream>
using namespace std ;
class Queue{
protected:
int rear ,front ;
int maxSize ;
int *elements ;
public:
Queue() {
}
void begin(int sz){
front = 0 ;
rear = 0 ;
maxSize = sz+1 ;
elements = new int [maxSize];
}
bool IsEmpty () {
return (front == rear ) ? true : false ;
}
bool IsFull () {
return((rear+1 ) %maxSize == front )? true :false ;
}
bool EnQueue (const int & x) {
if(IsFull() == true ) {
cout << endl;
cout <<"Full OV";
cout <<endl;
return false ;}
elements[rear] = x ;
rear = (rear+1)%maxSize ;
return true ;
}
bool DeQueue( ) {
if(IsEmpty() == true ) {
cout <<endl;
cout<<"Empty OV";
cout << endl;
return false ;
}
front = (front +1)%maxSize ;
return true ;
}
void print() {
if(IsEmpty () == true ) {
cout <<endl;
cout << "Empty" ;
}
else{
for(int i =front ; i!= rear ; i=(i+1)%maxSize ) {
cout << elements [i]<<" ";
}
}
}
};
void test(Queue& a , int choice) {
switch (choice) {
case 1: {
int size ;
cin >>size ;
a.begin(size) ;
}
break ;
case 2:{
int number ;
cin >> number ;
a.EnQueue(number) ;
}
break ;
case 3: {
a.DeQueue( ) ;
}
break ;
case 4:{
a.print() ;
}
break ;
default:
cout <<"wrong"<< endl;
break ;
}
}
int main () {
Queue a ;
int choice ;
while (cin >>choice) {
test(a,choice) ;
}
return 0 ;
}