用循环队列模拟银行窗口排队
#define SIZE 6
#include
#include<stdlib.h>
using namespace std;
typedef int ElemType;
typedef struct
{
ElemType *base;
int front,rear;
}SqQueue;
void Init(SqQueue &Q){
Q.base=(ElemType )malloc(SIZEsizeof(ElemType));
if(!Q.base){
exit(-1);
}
Q.front=0;
Q.rear=0;
}
void Output(SqQueue Q){
cout<<“队列内元素:”;
for(int i=Q.front;i<Q.rear;i++){
cout<<Q.base[i]<<" ";
}
cout<<endl;
}
void GetLength(SqQueue Q,int &length){
if(Q.front<=Q.rear){
length=Q.rear-Q.front;
}else{
length=SIZE-(Q.front-Q.rear);
}
}
void Enter(SqQueue &Q,ElemType e){
int len;
GetLength(Q,len);
if(len==SIZE-1){
cout<<“队列已满”<<endl;
return;
}
Q.base[Q.rear]=e;
if(Q.rear<SIZE-1){
Q.rear++;
}
else{
Q.rear=0;
}
Output(Q);
}
void Remove(SqQueue &Q,ElemType &e){
if(Q.front==Q.rear){
cout<<“队列一孔”<<endl;
return;
}
e=Q.base[Q.front];
if(Q.front<SIZE-1){
Q.front++;
}else{
Q.front=0;
}
Output(Q);
}
int main()
{int a,i,checks;
SqQueue Q;
Init(Q);
cout<<“队列总容量为:”<<SIZE-1<<endl<<endl;
cout<<“操作1:入队”<<endl;
cout<<“操作2:出队”<<endl;
cout<<“操作0:结束”<<endl;
while(true){
cout<<“请输入操作:”;
cin>>checks;
if(checks1){
cout<<“请输入元素:”<<endl;
cin>>a;
Enter(Q,a);
}
else if(checks2){
cout<<“请输入元素:”<<endl;
cin>>a;
Remove(Q,a);
}
else if(checks==0){
break;
}
else{
cout<<“非法操作”<<endl;
}
system(“pause”);
}
}