题目描述
队列操作题。根据输入的操作命令,操作队列(1)入队、(2)出队并输出、(3)计算队中元素个数并输出。
输入
第一行一个数字N。 1<=N<=50
下面N行,每行第一个数字为操作命令(1)入队、(2)出队并输出、(3)计算队中元素个数并输出。
即:
输入1,表示入队操作,随后输入一个数字并入队。
输入2,表示队首数字出队,并输出这个数字。
输入3,表示计算队中元素个数并输出。
输出
输出若干行。
每行显示一个2或3命令的输出结果。
注意:“2”出队命令可能会出现空队出队(下溢),请输出“no”,并退出。
样例输入 Copy
7 1 19 1 56 2 3 2 3 2
样例输出 Copy
19 1 56 0 no
#include<iostream>
#include<fstream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW - 2
#define MAXQSIZE 100
typedef int QElemType;
typedef int Status;
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue &Q)
{
Q.base=new QElemType[MAXQSIZE];
if(!Q.base)
exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
int QueueLength(SqQueue Q)
{
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%MAXQSIZE==Q.front)
return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
return OK;
}
Status DeQueue(SqQueue &Q,QElemType &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
return OK;
}
Status Empty(SqQueue Q)
{
if(Q.front==Q.rear)
return OK;
else
return ERROR;
}
int main()
{
SqQueue Q;
InitQueue(Q);
int n,m,flag;
cin>>n;
while(n)
{
cin>>m;
if(m==1)
{
cin>>flag;
EnQueue(Q,flag);
}
if(m==2)
{
if(Empty(Q))
cout<<"no"<<endl;
else
{
DeQueue(Q,flag);
cout<<flag<<endl;
}
}
if(m==3)
{
cout<<QueueLength(Q)<<endl;
}
n--;
}
return 0;
}