昨天苏州oj爆掉了,就没发,今天补上,利用的是循环队列来做的
内容:
以下是代码
#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 ;
}
原文地址:http://blog.csdn.net/hhooong/article/details/43193965