Linked List only displays first node

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

Here are my append and display functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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";
                
            }
        }
    
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void ColorsList::printList() const
 {
     ListNode *nodePtr;
     nodePtr = head;
    
    if(!nodePtr)
    {
        cout<<"The list is empty"<<endl;
    }
    else{
        while(nodePtr != nullptr)
        {
            cout<< nodePtr-> colorType<<endl;
            
            nodePtr = nodePtr->next;
        }
        
    }
    
 }


Here's how I'm calling it in main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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; 
    }
    
    
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
            //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";
                
            }
This code is incorrect. Can you figure out why?
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.
Is there anything that's inside the loop that should be after the loop?
Ahhh, I see the error now. Thanks for the help :)
Topic archived. No new replies allowed.