【发布时间】: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);
}
【问题讨论】:
-
问题出在命令 OUT 的主函数中,如果有人不知道发生了什么,我只会发布整个代码
-
请点击链接仔细阅读。然后告诉你程序的预期和实际输出在哪里?
-
@AnttiHaapala stackoverflow.com/questions/48357275/… 有输出
-
不,没有。请edit这个问题,以便它是独立的。
标签: c linked-list queue