c++分段错误

c++ Segmentation fault

本文关键字:错误 分段 c++      更新时间:2023-10-16

我正试图编译我的代码,但一旦我尝试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;

    }