c++分段错误
c++ Segmentation fault
我正试图编译我的代码,但一旦我尝试InsertNode,我就会收到一个分段,程序就会崩溃。请帮助
#include <iostream>
#include "Link.h"
using namespace std;
Node *createNode (){
Node *newNode = new Node;
cout<<"Enter your first name"<<endl;
cin >> newNode->firstName;
cout<<"Enter your last name"<<endl;
cin>>newNode->lastName;
cout <<"Enter your ID Number"<<endl;
cin>>newNode->idNumber;
newNode->next=NULL;
return newNode;
}
Node *insertNode (Node *list){
Node *NewNode = createNode();
//Node *NewNode = new Node;
// NewNode= createNode();
if(list == NULL){
list=NewNode;
}
else{
Node *tmp = list;
while(tmp->next!=NULL)
tmp = tmp->next;
tmp->next=NewNode;
}
return list;
}
Node *searchNode (Node *list){
bool found=false;
Node *tmp=NULL;
int ID;
cout << "Enter the ID you wish to search for: "<< endl;
cin >> ID;
if(list==NULL){
cout << "List is empty"<<endl;
return 0;
}
while(list->next!=NULL){
if(ID == (list-> idNumber)){
tmp=list;
found=true;
}
}
if(found=false){
cout<<"Not found"<<endl;
return 0;
}
return tmp;
}
Node *deleteNode (Node *list){
int ID;
if(list==NULL){
cout <<"The list is empty"<<endl;
return 0;
}
cout << "Enter the ID number you wish to delete:" << endl;
cin >> ID;
if(list->idNumber==ID){
Node *temp;
temp=list->next;
//free(list);
return temp;
}
list->next = deleteNode(list->next);
return list;
}
void printList(Node *list){
Node* tmp=list;
if(tmp==NULL){
cout<<"The list is empty"<<endl;
}
cout<< tmp-> firstName<<endl;
cout << tmp->lastName<<endl;
cout <<tmp->idNumber<<endl;
}
提前感谢您对的任何帮助
他们会得到一个菜单,用于调用这些函数中的任何一个添加我的main只是为了给你看
#include "Link.h"
#include <iostream>
using namespace std;
void DisplayMenu();
int main(){
int answer=0;
Node *NewNode = new Node;
//NewNode = NULL;
do{
DisplayMenu();
cin >> answer;
if(answer==1){
insertNode(NewNode);
}
else if(answer==2){
deleteNode(NewNode);
}
else if(answer==3){
printList(NewNode);
}
else if(answer==4){
searchNode(NewNode);
}
else if(answer==5){
cout << "Goodbye" << endl;
}
}while(answer!=5);
return 0;
}
void DisplayMenu(){
cout<< "1. Insert a node"<<endl;
cout<<"2. Delete a node"<<endl;
cout<<"3. Print List"<<endl;
cout<<"4. Search a node-search a node and print information for a student."<<endl;
cout<<"5. Quit the program"<<endl;
}
Node.next从未初始化为null。
这导致insertNode()中出现问题:
while(tmp->next!=NULL) <---- here
tmp = tmp->next;
试试这个代码,看看你是否能发现的差异
int main(){
int answer=0;
Node *list= NULL; //<---- note list variable renamed to 'list' and initialized to null
//NewNode = NULL;
do{
DisplayMenu();
cin >> answer;
if(answer==1){
list = insertNode(list); //<---- note added equals
}
else if(answer==2){
deleteNode(list);
}
else if(answer==3){
printList(list);
}
else if(answer==4){
searchNode(list);
}
else if(answer==5){
cout << "Goodbye" << endl;
}
}while(answer!=5);
return 0;
}
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 为什么我使用 std::copy() 收到运行时错误(分段错误)?
- 为什么我的代码包含错误分段错误(核心转储)?
- 为什么会出现此错误?- 分段错误(核心转储)
- 意外的运行时错误(分段错误)
- C++将文本文件读入数组,运行错误:分段错误
- mmap 错误:分段错误/指针无效错误
- 在我的代码中找不到错误....分段错误
- 错误:分段故障(核心转储)阵列
- 用string.h将错误分段到队列中
- c++字符串流转换错误:分段错误
- 为什么我的代码显示错误分段错误(核心转储)