模拟队列 数据结构 SzNOI c004

昨天苏州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


以下是代码

#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 ;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值