【问题标题】:Dequeue Queue one first then Queue two先出队队列一然后队列二
【发布时间】:2018-06-30 17:34:30
【问题描述】:

我想为每个 out 命令将 vip 队列中的客户出列...直到队列为空,如果我发出 OUT 命令,它应该开始普通出列,直到它为空,如果两个队列都是空的,我打印失败...........我的出队函数有问题,如果我先出队 VIP,它不会出队普通列表......但是如果我先出队普通人,它不会出队 VIP。当我调用它时,问题只存在于 main 的 Dequeue 函数中。

    #include <stdio.h>
    #include <malloc.h>
    #include<string.h>


    int length=1;//for counting number of customers per day

    typedef struct Node//customer details 
    {
        int record;
        int CardNum;
        char CustomerType[20];
        struct Node* next;

    }Node;

    typedef struct queue
    {
        Node* front;
        Node* rear;
    }Queue;

    Queue q1,q2;//two queues one vip one ordinary


    void Enqueue(Queue *q, int x, char *y);
    void Dequeue(Queue *q);

    int main()
    {
        q1.front=NULL;
        q2.front=NULL;
        q1.rear=NULL;
        q2.rear=NULL;

        char command[10];
        int card;
        char client[10];

        while(1)
        {
            scanf("%s",command);

            if(strcmp(command,"IN") == 0)
            {
                printf("IN:");

                scanf("%d",&card);
                scanf("%s",client);

                if(strcmp(client,"VIP")==0)//if client is vip push in queue1
                {
                    Enqueue(&q1,card,client);
                }
                else if(strcmp(client,"Ordinary")==0)//if client is vip push in queue1
                {               
                    Enqueue(&q2,card,client);
                }
            }

            else if(strcmp(command,"OUT") == 0)
            {/*i want to dequeue vip queue for every out command when the queue is empty if i make an OUT command it should start to dequeue ordianry untill they are done if both queues are empty i print FAILED*/
                if(q1.front == NULL && q1.rear == NULL && q2.front == NULL && q2.rear == NULL)
                {
                    printf("FAILED:\n");
                }

                else if(strcmp(q1.front->CustomerType,"VIP")==0)
                {
                    Dequeue(&q1);
                    position1--;
                }
                else if(strcmp(q2.front->CustomerType,"Ordinary")==0)
                {
                    Dequeue(&q2);
                    position2--;
                }
            }
            else if(strcmp(command,"QUIT") ==0)
            {
                printf("GOOD BYE!\n");
                break;
            }
        }
        return 0;
    }

    void Enqueue(Queue *q, int x, char *y)
    {
        Node* temp = (Node*)malloc(sizeof(Node));

        temp->CardNum=x;
        strcpy(temp->CustomerType,y);
        temp->record=length;
        temp->next=NULL;

        if(q->front == NULL && q->rear == NULL)
        {
            q->front=q->rear=temp;
        }
        else
        {
            q->rear->next=temp;
            q->rear=temp;
        }

        printf("%d %d %s %d\n",temp->record,temp->CardNum,temp->CustomerType);

        length++;
    }
    void Dequeue(Queue *q)
    {
        Node* temp;
        temp=q->front;

        if(q->front == q->rear)
        {
            q->front = q->rear = NULL;
            printf("OUT:%d %d %s\n",temp->record,temp->CardNum,temp->CustomerType);
        }
        else
        {
            q->front = q->front->next;
            printf("OUT:%d %d %s\n",temp->record,temp->CardNum,temp->CustomerType);

        }
        free(temp);

    }

【问题讨论】:

  • 请提供正确的minimal reproducible example
  • 问题出在命令 OUT 的主函数中,如果有人不知道发生了什么,我只会发布整个代码
  • 请点击链接仔细阅读。然后告诉你程序的预期实际输出在哪里?
  • 不,没有。请edit这个问题,以便它是独立的。

标签: c linked-list queue


【解决方案1】:

(这是显然是答案的评论,原来的答案不正确)

如果您的 VIP 队列 (q1) 为空,而普通队列 (q2) 不为空,则 q1.front 为空,但尽管如此,您仍试图读取 q1.front->CustomerType。这将崩溃或给出未定义的行为。

命名 vipQueue 和 normalQueue 而不是 q1 和 q2 会使代码更容易理解。

【讨论】:

  • 我想出列所有 VIP 的列表,然后每次调用函数出列的所有普通列表,如果 vip 列表为空,然后开始出列普通直到它也完成
  • 假设 VIP 队列为空:q1.front == q1.rear == null。假设普通队列不为空:q1.front != null && q1.rear != null。您检查 q1.front == NULL && q1.rear == NULL 的测试将不正确,因为 q2.front 和 q2.rear != NULL。代码会检查 strcmp(q1.front.....) 但 q1 为空,所以这可能会崩溃。
  • 是的,只有当两个队列都为空时,它才应该打印 FAILED,但如果 VIP 为空,它应该出列普通队列,直到两个队列都为空
  • 再次 - 如果您的 ViP 为空,则 q1.front 为空,但尽管您尝试读取 q1.front->CustomerType。这将崩溃或给出未定义的行为。
  • WOOOOW 非常感谢你,我对这个问题感到士气低落,每个人都没有得到我,我不知道如何解释谢谢你的耐心
猜你喜欢
  • 2017-04-11
  • 1970-01-01
  • 2016-01-19
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多