【发布时间】:2019-12-03 16:48:36
【问题描述】:
我用 C 语言编写了一个优先级队列,它通过使用优先级来接受航班的乘客信息。有三个主要的机票舱位(商务舱、经济舱、标准舱),商务舱有一个特殊舱位,即“外交官”,经济舱也有“老兵”舱位。以下是输入信息列表,括号中是相应的优先级:
输入: bus_1(1)、eco_1(3)、bus_2(1)、eco_2(3)、std_1(4)、eco_3(3)、eco_4(3)、bus_3(0 )、bus_4(1)、eco_6(2)、eco_7(2)
输出: bus_3、bus_1、bus_4、bus_2、eco_7、eco_6、eco_4、eco_3、eco_2、eco_1、std_1
应该是什么: bus_3、bus_1、bus_2、bus_4、eco_6、eco_7、eco_1、eco_2、eco_3、eco_4、std_1
0 是最高优先级,4 是最低优先级。我知道我的代码不正确,但我无法找出正确的方法来编写算法以将相同的优先级项目推送到已经在队列中的项目之后。这是我的功能:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
enum classes
{
business,
economy,
standard
};
struct flight
{
char flightName[8];
//for priority queue
struct queueNode *rootNode;
int hasRoot;
int businessQueueCount, economyQueueCount, standardQueueCount;
int totalPassengerCount;
};
struct passenger
{
char passengerName[15];
char flightName[8];
};
struct queueNode
{
struct passenger passenger;
int priority;
struct queueNode *next;
};
struct queueNode *newQueueNode(char flightName[8], char passengerName[15], int priority)
{
struct queueNode *temp = malloc(sizeof(struct queueNode));
temp->priority = priority;
temp->next = NULL;
strcpy(temp->passenger.flightName, flightName);
strcpy(temp->passenger.passengerName, passengerName);
return temp;
}
void pushQueue(struct queueNode **head, char *flightName, char *passengerName, int priority)
{
struct queueNode *start = (*head);
struct queueNode *temp = newQueueNode(flightName, passengerName, priority);
if ((*head)->priority > priority)
{
temp->next = *head;
(*head) = temp;
}
else
{
if (start->next != NULL && start->next->priority == priority)
{
temp->next = start->next->next;
start->next->next = temp;
}
else
{
while (start->next != NULL && start->next->priority < priority)
{
start = start->next;
}
temp->next = start->next;
start->next = temp;
}
}
}
struct passenger peekQueue(struct queueNode **head)
{
return (*head)->passenger;
}
void popQueue(struct queueNode **head)
{
struct queueNode *temp = *head;
(*head) = (*head)->next;
free(temp);
}
int main(){
struct flight flight_temp;
strcpy(flight_temp.flightName, "flight1");
flight_temp.rootNode = newQueueNode(flight_temp.flightName, "bus_1", 1);
pushQueue(&(flight_temp.rootNode), flight_temp.flightName, "eco_1", 3);
pushQueue(&(flight_temp.rootNode), flight_temp.flightName, "bus_2", 1);
pushQueue(&(flight_temp.rootNode), flight_temp.flightName, "std_1", 4);
pushQueue(&(flight_temp.rootNode), flight_temp.flightName, "eco_3", 3);
pushQueue(&(flight_temp.rootNode), flight_temp.flightName, "bus_3", 0);
pushQueue(&(flight_temp.rootNode), flight_temp.flightName, "bus_4", 1);
pushQueue(&(flight_temp.rootNode), flight_temp.flightName, "eco_4", 3);
for (size_t i = 0; i < 6; i++)
{
printf("%s\n", peekQueue(&(flight_temp.rootNode)).passengerName);
popQueue(&(flight_temp.rootNode));
}
}
提前感谢您的帮助。
【问题讨论】:
-
您的队列代码似乎部分基于相同优先级的 LIFO(而您所需的输出似乎需要基于 FIFO 以匹配优先级)。很难说没有实际显示您的优先级计算的minimal reproducible example(必须有一些东西somewhere为每个给定的类和优先级调整
priority,但你没有在这里显示它) .把一个 mcve 放在一起,我们可能可以解决这个问题。无论如何,是时候启动调试器并开始单步执行了。使用minimal reproducible example 更新您的代码,包括输入格式的输入数据、插入等。 -
@Klaus 感谢您的回答。据我从您推荐的那个问题中了解到,我必须添加另一个功能来做我想做的事。但是我不知道该怎么做,因为我没有任何时间戳等。对我来说最好的解决方案是在不添加其他区域的情况下保持与 FIFO 相同的优先级。
-
@WhozCraig 我准备好了你问的,我应该直接将我的问题添加为代码吗?
-
我很确定您需要做的就是将
while (start->next != NULL && start->next->priority < priority)中的<更改为<=。
标签: c priority-queue