问题遇到的现象和发生背景
给定一个初始为空的队(队存储空间长度为10)和一系列进队、出队操作,请编写程序输出经过这些操作后队中的元素。队中元素值均为整数。(采用循环队列完成,禁用一个空间方法)
输入格式:
输入第1行为1个正整数n,表示操作个数;
第2行为给出的n个整数,非0元素表示进队,且此非0值即为进队元素,0元素表示出队。
输出格式:
第一行按出队顺序输出所有出队元素,以一个空格隔开;如果队空时做出队操作会输出"EMPTY",如果队满时做进队操作会输出"FULL"。
第二行中输出队中所有元素,以一个空格隔开。
末尾均有一个空格。
问题相关代码,请勿粘贴截图
#include<iostream>
using namespace std;
#define ERROR 0
#define OK 1
#define MAXQSIZE 10
typedef int QElemtype;
typedef int status;
typedef struct
{
QElemtype *base;
int rear;
int front;
}SqQueue;
status InitQueue(SqQueue &Q)
{
Q.base = new QElemtype[MAXQSIZE];
if (!Q.base)
return OK;
}
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 OK;
else
{
return ERROR;
}
}
status IsEmpty(SqQueue &Q)
{
if (Q.front == Q.rear)
return OK;
else
return ERROR;
}
status IsFull(SqQueue &Q)
{
if ((Q.rear + 1) % MAXQSIZE == Q.front)
return OK;
else
return ERROR;
}
status DestroyQueue(SqQueue &Q)
{
free(Q.base);
Q.base = 0;
Q.front = 0;
if (Q.base == 0)
{
return OK;
}
else
{
return ERROR;
}
}
int main()
{
SqQueue Q;
int n, i, op, e;
InitQueue(Q);
printf("请输入操作次数");
cin >> n;
printf("请输入操作:");
for (i = 0; i < n; i++)
{
cin >> op;
if (op)
{
if (IsFull(Q))
cout << "FULL";
else
EnQueue(Q, op);
}
else
{
if (IsEmpty)
cout << "EMPTY";
else
{
DeQueue(Q,e);
cout << e << " ";
}
}
}
cout.put('\n');
while (!IsEmpty(Q))
{
DeQueue(Q, e);
cout << e << " ";
}
cout << endl;
DestroyQueue(Q);
return 0;
}
运行结果及报错内容
我的解答思路和尝试过的方法
不知道如何解决
我想要达到的结果
程序可以正常运行