c - C链表中的优先级队列

标签 c

我已经为优先级队列创建了这个程序,但遇到了问题。我得到了错误的输出。

这是输入:

Insert 10000 2
Insert 10000 2
Insert 10000 3
Insert 19444 9
Pop
Insert 10331 3
Pop
Pop
Pop
Pop
Pop

输出应该是这样的:

19444
10000
10331
10000
10000
-1

这是我得到的输出:

19444
10000
10000
10000
10331
-1

解决了!

最佳答案

我认为您的优先级检查逻辑不正确:

while (queue->next != NULL && queue->next->prior >= /* not <= */ priorty)

或者更好

while (queue->next != NULL && priorty <= queue->next->prior)

不确定您打算如何处理两个元素具有相同优先级的情况,但由于您的插入使用“大于”来替换队列的头部,您可能希望保持相同的逻辑。

关于c - C链表中的优先级队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36119756/

相关文章:

c - 即使包含 <stdlib.h> 也会出现警告 : implicit declaration of function ‘malloc’ ,

c - 为什么在 malloc() 导致 free() : invalid next size (fast) 之后无效的 memset()

c - 没有 undef 的宏名称转义

c++ - 如何找出键盘的地址

C 按位或意外更改值

c - 是否需要将 free() 参数转换为 void *?

c - 解析 mac 地址和移动 temp 时检查 strtol() 时出错。指针向上

c - 结构数组作为参数传递给 read() 问题

c - 大阵列上的段错误

c - C中的符号常量和宏有什么区别?