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;
}

提前感谢您的帮助

给他们一个菜单,用于调用这些功能中的任何一个 添加我的主要内容只是为了向您展示

#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;


    }