我在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));
}
}
在此先感谢您的帮助。
答案 0 :(得分:0)
本质上,您的 //declare these
PageController _pageController = PageController(viewportFraction: 0.5);
int selectedPage = 0;
List yourList =[];
//in initState method
_pageController.addListener(() {
setState(() {
selectedPage = controller.page;
});
});
//inside body
PageView(
controller: _pageController,
itemBuilder: (context, index) {
if (index == selectedPage) {
Container(
alignment: Alignment.center,
height:300.0,
width:300.0,
child: CircleAvatar(
backgroundImage: yourList[index],
//modify as you want
)
)
}
else {
Container(
alignment: Alignment.bottom,
height:100.0,
width:100.0,
child: CircleAvatar(
backgroundImage: yourList[index],
//modify as you want
)
}
},
itemCount: yourList.length,
),
函数正尝试将其插入已排序列表中,请确保如果已经存在相等的项,则新项将紧随其后。但是您的代码中存在一些使其失败的问题。我已经在您的代码中添加了一些注释,以向您显示错误的位置。
pushQueue
通过组合案例,可以大大简化代码。而且也摆脱了难以理解且容易出错的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;
// if you put a return statement here, then you can get rid of
// the else. It reduces nesting and makes your code cleaner.
}
else
{
// If the new item has the same priority as what's already in the list,
// then you insert the new item right after it. What if there were two
// or more items with the same priority? The new one wouldn't go
// to the end of the list.
// This code really is just a broken special case for the else below.
if (start->next != NULL && start->next->priority == priority)
{
temp->next = start->next->next;
start->next->next = temp;
}
else
{
// You want to insert the new item after any existing items that
// have the same priority. But your logic will stop when it finds
// the first node that has the same priority. So the new item
// gets inserted at the front. You need to change '<' to '<='
while (start->next != NULL && start->next->priority < priority)
{
start = start->next;
}
temp->next = start->next;
start->next = temp;
}
}
}
之类的弯头表达式,从而导致尝试取消引用NULL指针。
start->next->next
您当然要进行测试。我并没有真正改变您的算法:只是将您的两种特殊情况组合为一种。