优先队列的插入和显示功能
priority queue INSERTION and DISPLAY function in C++
尝试实现优先级队列插入(dev c++ IDE),即在具有优先级的节点之前>新节点的优先级。来自用户的插入似乎发生在命令提示符上,但display函数只输出第一个节点。为什么没有显示其他队列元素?我担心它们没有首先被插入,这就是为什么在随后插入10、20、30,然后删除时,它打印空队列。因为队列只有10个人,为什么?请建议。
注意:我没有包括delete和main函数,这里的代码,但它在程序中,显然…没有运行时异常发生…编译工作正常。但是没有期望的输出。
#include<iostream>
using namespace std;
typedef struct node{ //template would enable generic datatype
int info,prn;
node * link;
}node;
node * start = 0;
int item,pri;
void insert(int item, int pri){
node * newnode = new node;
newnode-> info = item;
newnode-> prn = pri;
if(start == 0){//explicit check in insert if start is NULL.
start = newnode;
newnode->link = 0;
}
/*When you first run the program, start = 0 or NULL. You then say prev = start,
so as a result prev = NULL. So when you try to access prev->link, there's an access violation.*/
else{
node * prev= start;
node * temp = prev->link;
while (temp!= 0){
if(temp->prn > newnode->prn){
newnode -> link = prev -> link;
prev -> link = newnode;
break;
}
else{
if( temp->link == 0){
temp -> link = newnode;
newnode->link = 0;
break;
}
}
prev = prev->link;
}
}
}
void display(){
if(start == 0)
cout<<"Empty priority queuen";
else{
cout<<("The Contents of the List are: ");
node *temp = start;
while(temp!=NULL){ //if we do while temp->link!=NULL, then last node won't print
cout<< temp->info;
cout<<" ---> ";
temp = temp->link;
}
}
}
第一次调用insert时,start将为0,但是您将把它赋值给prev。在下一行,给出错误的代码调用prev->link
,因此c++在调用NULL(调用未定义行为)上的方法时崩溃。如果start
为NULL,则应该在insert中添加显式检查。
第一次运行程序时,start = 0或NULL。然后输入prev = start,因此prev = NULL。所以当你试图访问prev->link时,就会出现访问冲突
相关文章:
- 链表中的显示功能无法正常工作
- OpenGL Glut显示功能在Mojave上显示黑屏
- 为什么我的显示功能会终止程序?
- 需要帮助操作员过载,这样我才能显示功能
- 氧气显示功能简要概述
- 我的显示功能有问题
- 休息后显示功能
- 由于显示功能而引起的非法间接误差
- 体系结构x86_64的未定义符号-头文件中的显示功能
- 在Visual Studio 08中悬停鼠标时显示功能信息
- 多通道垫子显示功能
- 显示功能不显示数据
- 链表C++,输出未显示在显示功能中
- 单链表显示功能分段故障
- c++ 2d array显示功能
- 优先队列的插入和显示功能
- 从而创建显示功能
- BigInt显示功能
- 显示功能不起作用
- 调试断言失败!在人脸识别打开Cv的检测和显示功能中