我已经为优先级队列创建了这个程序,但遇到了问题。我得到了错误的输出。
这是输入:
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/