沉迷摸鱼无法自拔 2022-05-27 22:27 采纳率: 70.6%
浏览 126
已结题

请问这个问题如何解决

问题遇到的现象和发生背景

给定一个初始为空的队(队存储空间长度为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;
}

运行结果及报错内容

img

我的解答思路和尝试过的方法

不知道如何解决

我想要达到的结果

程序可以正常运行

  • 写回答

1条回答

  • qfl_sdu 2022-05-27 22:51
    关注

    (1)初始化时,没有初始化rear和front
    (2)出队逻辑写错了
    修改后运行结果:

    img

    代码修改如下:

    #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];
        Q.rear = 0;
        Q.front = 0;
        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 ERROR;
        else
        {
            Q.rear -= 1;
            e = Q.base[Q.rear];
            return OK;
        }
    }
    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(Q))
                    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;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月6日
  • 已采纳回答 5月29日
  • 创建了问题 5月27日

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题