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;
}
PrintListfunction trashes the only pointer you have to your list, is that really what you want?