Hey ya'll, I'm new to linked lists and I'm having some toruble with displaying my list. I created a function which appends nodes to the list, as well as a display function, which should display the entire list, however, only the first node is displayed. I would appreciate any help thanks
void ColorsList::appendNode(string color)
{
//Will point to a new node
ListNode *newNode;
//Pointer used to traverse though list
ListNode *nodePtr;
//create a new node
newNode = new ListNode;
//strore color in new node
newNode-> colorType = color;
//will point to last node of list
newNode-> next=nullptr;
//Mutator validation
if(color != "red" && color != "yellow" && color !="blue")
{
cout<<"Invalid color. Enter a primary color.\n Ex: red, yellow, or blue\n";
exit(EXIT_FAILURE);
}
//If there are no nodes in list
//Make the new node the first node in list
if(!head)
{
//make newNode the first node in list
head = newNode;
cout<<"Now first node in list\n";
}
else
{
//Set nodePtr to the beginning of list
nodePtr = head;
//Finding last node in list
while (nodePtr->next)
{
//set nodePtr to the next node in list
nodePtr = nodePtr->next;
//set newNode to the node nodePtr is pointing to
nodePtr->next = newNode;
cout<<"Node Created at the end of list\n";
}
}
}
int main()
{
//ColorsList test;
//test.appendNode("Red");
//cout<<"node appended";
int user_input;
ColorsList color;
do{
display_menu();
user_input = get_user_choice();
switch(user_input)
{
case 1:
color.appendNode(validateUserInput());
break;
case 2:
cout<<"insert method";
break;
case 3:
cout<<"deletenode method";
break;
case 4:
color.printList();
break;
case 5:
cout<<"ReverseList method";
break;
case 6:
cout<< "SearchList method";
break;
case 7:
color.exitMenu();
break;
default:
color.invalidInput();
break;
}
}while(user_input != 7);
return 0;
}
int get_user_choice()
{
//will hold the user's choice
int user_choice = 0;
//reading in user choice
cin >> user_choice;
cin.ignore();
return user_choice;
}
void display_menu()
{
cout<< "\n"<<"Please enter a value (1-7)\n"<<"1. Append\n" << "2. Insert\n" << "3. Delete\n" <<
"4. Print\n"<< "5. Reverse\n" <<"6. Search\n"<<"7. Exit\n"<<endl;
}
//Method used for high level validation
string validateUserInput()
{
string user_input;
cout<<"Please enter a color you'd like to add to your list\n";
cout<<"Your choices are red, yellow, and blue\n";
getline(cin, user_input);
for(int i =0; i <= user_input.length(); i++)
{
user_input[i] = tolower(user_input[i]);
}
if(user_input != "red" && user_input != "yellow" && user_input != "blue")
{
cout<< user_input<< " is not a color that is allowed"<<endl;
}
else
{
return user_input;
}
}
//Set nodePtr to the beginning of list
nodePtr = head;
//Finding last node in list
while (nodePtr->next)
{
//set nodePtr to the next node in list
nodePtr = nodePtr->next;
//set newNode to the node nodePtr is pointing to
nodePtr->next = newNode;
cout<<"Node Created at the end of list\n";
}
I'm not certain, but my guess would be because nodePtr isn't pointing to the last node in the list, which is why anything after the first node doesn't show up.