咔咔_ 2022-03-11 22:32 采纳率: 100%
浏览 89
已结题

银行排队问题,要求用队列解决,但我检查了好久,也没发现有什么问题

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

CLZ 银行只有两个接待窗口,VIP 窗口和普通窗口,VIP 用户进入VIP 窗口排队,剩下的进入普通窗口排队.
现有M 次操作,操作有四种类型,如下:
IN name V:表示一名叫 name 的用户到 VIP 窗口排队
OUT V:表示 VIP 窗口队头的用户离开排队
IN name N:表示一名叫 name 的用户到普通窗口排队
OUT N:表示普通窗口队头的用户离开排队
求 M 次操作结束后 VIP 窗口队列和普通窗口队列中的姓名。
输入描述:第一行是一个整数 M 格式如下:
IN name V
OUT V
IN name N
OUT N
输出描述:
输出 M 次操作后 VIP 窗口队列和普通窗口队列中的姓名(从头到尾),先输出 VIP 窗口队列后输出普通窗口队列。

问题相关代码,请勿粘贴截图
#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct queue
{string data[100];
int front,rear;
}Queue;
void InitQueue(Queue *&q)
{q=(Queue *)malloc(sizeof(Queue));
q->front=q->rear=0;
}
bool enQueue(Queue *&q,string name)
{
        q->data[q->rear]=name;
        q->rear++;
        return true;
}
bool deQueue(Queue *&q)
{   
    if(q->front==q->rear)
    return false;
    q->front++;
    return true;
}
string get(Queue *q,string &e)
{
    e=q->data[q->front];
    return e; 
}
int main()
{int m;
string e,h;
cin>>m;
Queue *VQ;
Queue *NQ;
InitQueue(VQ);
InitQueue(NQ);
while(m--)
{
    string one,name,type;
    cin>>one;
    if(one=="IN")
    {  
        cin>>name>>type;
        if(type=="V")
        enQueue(VQ,name);
        else
        enQueue(NQ,name);
     }
     else
     {cin>>type;
      if(type=="V")
      deQueue(VQ);
      else
      deQueue(NQ); 
    }}
    get(VQ,e);
    get(NQ,h);
    while(deQueue(VQ))
    {get(VQ,e);
    cout<<e<<" ";
    }
    while(deQueue(NQ))
    {get(NQ,h);
    cout<<h<<" ";
    }
    cout<<endl;
    return 0;
}

运行结果及报错内容

没有输出结果

我的解答思路和尝试过的方法
我想要达到的结果

操作完成后输出队列中的值

  • 写回答

4条回答 默认 最新

  • 关注

    malloc()不能分配包含 string 类型的结构体

    q = (Queue *)malloc(sizeof(Queue));
    

    改成

    q = new Queue;
    

    你题目的解答代码如下:

    #include <iostream>
    #include <malloc.h>
    using namespace std;
    typedef struct queue
    {
        string data[100];
        int front, rear;
    } Queue;
    void InitQueue(Queue *&q)
    {
        q = new Queue; //***************
        q->front = q->rear = 0;
    }
    bool enQueue(Queue *&q, string name)
    {
        q->data[q->rear] = name;
        q->rear++;
        return true;
    }
    bool deQueue(Queue *&q)
    {
        if (q->front == q->rear)
            return false;
        q->front++;
        return true;
    }
    string get(Queue *q, string &e)
    {
        e = q->data[q->front];
        return e;
    }
    int main()
    {
        int m;
        string e, h;
        cin >> m;
        Queue *VQ;
        Queue *NQ;
        InitQueue(VQ);
        InitQueue(NQ);
        while (m--)
        {
            string one, name, type;
            cin >> one;
            if (one == "IN")
            {
                cin >> name >> type;
                if (type == "V")
                    enQueue(VQ, name);
                else
                    enQueue(NQ, name);
            }
            else
            {
                cin >> type;
                if (type == "V")
                    deQueue(VQ);
                else
                    deQueue(NQ);
            }
        }
        get(VQ, e);
        cout << e << " "; //获取之后要输出
        while (deQueue(VQ))
        {
            get(VQ, e);
            cout << e << " ";
        }
        cout << endl;
        get(NQ, h);
        cout << h << " "; //获取之后要输出
        while (deQueue(NQ))
        {
            get(NQ, h);
            cout << h << " ";
        }
        cout << endl;
        return 0;
    }
    
    

    img

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月14日
  • 已采纳回答 3月11日
  • 创建了问题 3月11日