Traversing list is not showing expected number of nodes.

Hello, I wrote the method to add nodes to singly linked-list. In this case adding 10 nodes to the linked-list. Then I attempt to traverse and display how many nodes are added in the list. It displays only two nodes though. Can someone help me find out what I am missing. Probably I am adding a nullptr one of the nodes and the while loop is finishing at that point...

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include "list.h"

using namespace std;

List::List()
{
	head = nullptr;

}

int List::add(int n)
{
	count = 1;
	head = new node;  //Create a node (first node is head)
	head->data = 1;   //Assign value 1 to the node
	head->next = nullptr; //Since that is the only node in this list, it points to a nullptr.

	//Now that the first element (head) has been created, start creating other nodes...
	for (int i = 2; i <= n; i++)
	{
		//Start assigning data to the other nodes
		node* other_node = new node;
		if (i == 2)
			cout << "I am here in the second node...";
			//If this is the second node, assign head to the other_node. Therefore head->next no longer pointing to 'nullptr'
			head->next = other_node;
	
		other_node->data = i; //assign data value i to the node
		other_node->next = nullptr; //Since the other node is now the last element in the list, 
									//make it point to nullptr
		other_node->prev = other_node; //The previous node now should point to this 'current', other_node
		
		cout << "I am here at node 3 and beyond..." << endl;
		count++;
		
	}
	
	return count;
}

void List::search(int n)
{
	//Search a node position given its position id.
	
}

void List::remove(int n)
{
	//Remove a node based on user input position id.
	//After the removal, print to console list of nodes to demonstrate list is up to date.
}

int List::display()
{
	node* cur = head;
	int count = 0;
	while (cur != nullptr)
	{
		cout << "** I AM HERE on DISPLAY ...";
		cur = cur->next; //browse to next hop
		count++;
	}

	return count;

}

//========================= list.h below====================================
#include <iostream>

struct node
{
	int data;
	struct node* next;
	struct node* prev;
};

class List
{
private:

	struct node* head;
	int n; //user input  variable to determine how many nodes to create
	void remove(int n);
	int count;


public:

	int add(int n);
	void search(int n);
	List();
	int display();
	
};



//======================main.cpp=========================================

#include "list.h"
#include <iostream>

using namespace std;

int main()
{
	List list;    //initialize object 
	cout << "Number of nodes in add operation: " << list.add(10) << endl;  
	cout << "Number of items found in this list:"  << list.display() << endl;
};

//=================OUPUT===================================== 

I am here in the second node...I am here at node 3 and beyond...
I am here at node 3 and beyond...
I am here at node 3 and beyond...
I am here at node 3 and beyond...
I am here at node 3 and beyond...
I am here at node 3 and beyond...
I am here at node 3 and beyond...
I am here at node 3 and beyond...
I am here at node 3 and beyond...
Number of nodes in add operation: 10
** I AM HERE on DISPLAY ...** I AM HERE on DISPLAY ...Number of items found in this list:2 <== I was expecting 10 nodes
Last edited on
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
#include <iostream>

struct list
{
    // TO DO: destructor etc.

    void add( int value ) // add a node (at the front) with data == value
    {
        // next of this new node is the current head
        node* const new_node = new node { value, head } ;

        // and the new node becomes the new head
        head = new_node ;
    }

    void print() const
    {
        std::cout << "items in the list: [ " ;

        for( node* n = head ; n != nullptr ; n = n->next )
            std::cout << n->data << ' ' ;
        std::cout << "]\n" ;
    }

    private:

        struct node
        {
            int data ;
            node* next ;
        };

        node* head = nullptr ;
};

int main()
{
    list lst ;

    int cnt = 0 ;
    std::cout << "how many items do you want to add? " ;
    std::cin >> cnt ;

    // add cnt items to the list
    for( int i = 0 ; i < cnt ; ++i ) 
    {
        std::cout << "\nadding " << i << '\n' ;
        lst.add(i) ;
        lst.print() ;
    }
}
Good work JLBorges. Thanks.
Topic archived. No new replies allowed.