2

I currently have this code for my node class which stores two names, an ID number, and has a Next ability:

class Node{
public:
  char LastName[20] ;
  char FirstName[20] ;
  int IDnumber ;
  Node *Next ;

 Node();
 void printNode();
};

This is the constructor I am using to initialize the node variables from the keyboard:

Node::Node(){

cout << "Enter ID number: " << endl;
cin >> IDnumber;
cout << "Enter last name: " << endl;
cin >> LastName;
cout << "Enter first name: " << endl;
cin >> FirstName;
Next=NULL;
}

void Node::printNode(){
cout << "ID number: " << IDnumber << endl;
cout << "Last name: "  << LastName <<endl;
cout << "First name: "  << FirstName << endl;
}

The problem I am having is that whenever i call the printNode() function later in my code, my code fails to execute the first line of the printNode() function. (unhandled exception) I also fail to execute this code when I attempt to call node->Next with my separate linkedlist class. This leads me to believe that I am not constructing the node correctly. any ideas on what could be wrong in my code?

The linked list is a separate class that uses the node class I posted above.

class LinkedList{

private:
Node* list;
Node* createNode();
Node* searchLocation(int);

public:

LinkedList();
~LinkedList();

void InsertNode();
void SearchNode();
void PrintList();
void DeleteNode(int);

};


LinkedList::LinkedList(){
Node* list = NULL;
}

Node* LinkedList::createNode(){
Node *NewNode = new Node();
return NewNode;
}

void LinkedList::InsertNode(){
Node* insert = createNode();
if (list == NULL){
    list = insert;}}

void LinkedList::PrintList(){
Node* temp = list;
while (temp != NULL){
temp->printNode();
temp = temp->Next;
}
}

the PrintList() function of my LinkedList class fails when list->printNode() (there is a break at the cout << IDnumber line) and also fails at the list = list->Next line.

int main(){

int num = 0;
    LinkedList list;

  int menu=0;
  while(menu != 5){

  cout << endl << "Choose a menu option." <<endl
   << "1. Insert node " << endl << "2. Delete node " << endl
   << "3. Print list" << endl << "4. Search a node & print info" << endl
   << "5. Quit program  " << endl;


  cin >>  menu;
   menu = validate(menu);

 switch(menu){
 case 1: 
     list.InsertNode();

 break;

case 3: 
    list.PrintList();

 break;
 }}

return 0;

  }
11
  • please paste also how you create Node object and how you use it later Commented Sep 26, 2013 at 3:30
  • alrighty, i posted it Commented Sep 26, 2013 at 3:40
  • Your PrintList function trashes the only pointer you have to your list, is that really what you want? Commented Sep 26, 2013 at 3:41
  • no, I suppose I should create a temporary pointer. I went ahead and edited that in. Commented Sep 26, 2013 at 3:42
  • can you paste main() ? Commented Sep 26, 2013 at 3:43

1 Answer 1

1

you have few errors in your code. most important you refer to local list pointer which is always NULL, when you should refer to some common Node, i.e. static variable.

here you can find working solution, please add body for correct list deallocation

~LinkedList(){}

and you are OK:

LinkedList:

class LinkedList{

private:
static Node* list;
Node* createNode();
Node* searchLocation(int);

public:
LinkedList();
~LinkedList(){}
void InsertNode();
void SearchNode();
void PrintList();
void DeleteNode(int);
};

Node* LinkedList::list = NULL;
                 ^
     don't foget to initialize pointer to static object

LinkedList::LinkedList(){
Node* list = NULL;
}

Node* LinkedList::createNode(){
Node *NewNode = new Node();
return NewNode;
}

void LinkedList::InsertNode(){
Node* insert = createNode();
if(list==NULL)list=insert;
else
    list->Next = insert;
}

void LinkedList::PrintList(){
Node* temp = list;
while (temp != NULL){
temp->printNode();
temp = temp->Next;
}
}

and main:

int main(){

int num = 0;
   LinkedList list;

   cout << endl << "Choose a menu option." <<endl
   << "1. Insert node " << endl << "2. Delete node " << endl
   << "3. Print list" << endl << "4. Search a node & print info" << endl
   << "5. Quit program  " << endl;

   list.InsertNode();
   list.InsertNode();

   list.PrintList();

return 0;

}

output: Choose a menu option. 1. Insert node 2. Delete node 3. Print list 4. Search a node & print info 5. Quit program
Enter ID number: 8 Enter last name: i Enter first name: j Enter ID number: 9 Enter last name: k Enter first name: l

ID number: 8

Last name: i

First name: j

ID number: 9

Last name: k

First name: l

RUN SUCCESSFUL (total time: 14s)

Sign up to request clarification or add additional context in comments.

1 Comment

Is there a reason to not use nullptr?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.