阅读背景:

实现队列的入队和出队操作

来源:互联网 

1)顺序队
#include <iostream>  
using namespace std;
const int MAX=10;  
class Queue{  private:    
int front;     
int rear;     
int data[MAX];  
public:  
  Queue(){front=rear=MAX-1;}  
   ~Queue(){} ;    
void EnQueue(int x);    
int DeQueue();   
  int GetQueue()
{if 
(front!=rear) return data[(front+1)%MAX];
};    
int Empty();  };  
void Queue::EnQueue(int x){  
   if ((rear-1)%MAX==front) throw "上溢"; 
rear=(rear+1)%MAX;    data[rear]=x;  
}  
int Queue::DeQueue(){    
if (front==rear) throw "下溢";   
int x=data[front];   
front=(front+1)%MAX;   
return x;
  }  
int Queue::Empty(){  
   if (front==rear) return 1; 
else return 0;  
}
int main(){
    Queue S;     
if (S.Empty())       
  cout<<"队为空!"<<endl;   
  else    
  cout<<"队不为空!"<<endl;  
  cout<<"15入队:"<<endl;   
S.EnQueue(15);     
cout<<"10入队:"<<endl;    
S.EnQueue(10);    
cout<<"队头元素为:"<<endl;   
cout<<S.GetQueue()<<endl;   
cout<<"执行一次出队操作:"<<endl;   
S.DeQueue();    
cout<<"队头元素为:"<<endl;   
cout<<S.GetQueue()<<endl;  
  return 0;
}  #include <iostream>  
usin



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: