Simple traversal function crashing

Hello, the 'add' function below seems to be creating nodes accordingly.
However, trying to display node is not working. It crashes when I attempt to hop to next node. Can someone confirm what is wrong?

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
  #include "list.h"
#include <iostream>

using namespace std;

List::List()
{
	head = nullptr;
	n = 0;
	count = 0;

}

void List::add(int n)
{
	cout << "** I AM HERE";
	head = new Node;
	head->data = 1;
	Node* prev_node = head;
	for (int i= 2; i <= n; i++)
	{
		Node* other_node = new Node;
		other_node->data = i;
		prev_node->next = other_node;
		prev_node = other_node;
		cout << "** CREATING NODE..." << endl;
	}
	
}

void List::display()
{

	Node* cur_node = head;
	
	while (cur_node != nullptr)
	{
		cur_node = cur_node->next;  //<== it is crashing here. I cannot understand why.
		cout << cur_node;
			   	
	}
	
 

}
//======== list.h  ===================================
struct Node
{
	int data;
	struct Node* next;
	
};

class List
{
	private:
		struct Node* head;
		int n;
		int count;
	
		 
		
		
	public:
		void add(int n);
		List();
		void display();
		
		 

};
It looks like your add() method is trying to add a node with data==1 at position n. Is that what it's supposed to do? Usually a method like this adds a node with data=n to the front or the back of the list.

In any case, line 17 points head at the new node, which blows away anything that was already in the list.

Please explain what add() is supposed to do and then we can help you code it correctly.
In display, you have checked that cur_node is not nullptr, but you haven't checked that cur_node->next isn't ... yet you try to print it out.

Reverse the order of lines 38 and 39. Then make sure that you print cur_node->data, not the pointer address.
Last edited on
Hello, Mr lastchance, I did as you suggested and it is still crashing.
dhayden, I am creating the node and adding elements to it. The first element has "1". Then the second element and beyond will receive value data=2, 3 etc.

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
#include "list.h"
#include <iostream>

using namespace std;

List::List()
{
	head = nullptr;
	n = 0;
	count = 0;

}

void List::add(int n)
{
	cout << "** I AM HERE";
	head = new Node;
	head->data = 1;
	Node* prev_node = head;
	for (int i= 2; i <= n; i++)
	{
		Node* other_node = new Node;
		other_node->data = i;
		prev_node->next = other_node;
		prev_node = other_node;
		cout << "** CREATING NODE..." << endl;
	}
	
}

void List::display()
{

	Node* cur_node = head;
	
	while (cur_node != nullptr)
	{
		cout << cur_node->data;
		cur_node = cur_node->next;   
	
			   	
	}

}
Well, here's your whole code in runnable form.

It's not great (how many n do you have?), but it doesn't crash.

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
#include <iostream>
using namespace std;

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

class List
{
	private:
		struct Node* head;
		int n;
		int count;
		
	public:
		void add(int n);
		List();
		void display();
};

List::List()
{
	head = nullptr;
	n = 0;
	count = 0;

}

void List::add(int n)
{
	cout << "** I AM HERE";
	head = new Node;
	head->data = 1;
	Node* prev_node = head;
	for (int i= 2; i <= n; i++)
	{
		Node* other_node = new Node;
		other_node->data = i;
		prev_node->next = other_node;
		prev_node = other_node;
		cout << "** CREATING NODE..." << endl;
	}	
}

void List::display()
{
	Node* cur_node = head;
	while (cur_node != nullptr)
	{
		cout << cur_node->data;
		cur_node = cur_node->next;   		   	
	}
}

int main()
{
    List Lst;
    Lst.add( 10 );
    Lst.display();
}


** I AM HERE** CREATING NODE...
** CREATING NODE...
** CREATING NODE...
** CREATING NODE...
** CREATING NODE...
** CREATING NODE...
** CREATING NODE...
** CREATING NODE...
** CREATING NODE...
12345678910 
Last edited on
When I ran the first time, it did not crash either. I am wondering if there is a memory leak somewhere that eventually builds up and end up crashing. I was running for 100 nodes.
Last edited on
I think you should initialise your next pointer (with nullptr).

1
2
3
4
5
struct Node
{
	int data;
	struct Node* next=nullptr;
};
Last edited on
I think you need a ‘push_back()’ function to be invoked by your add n elements method.
Example:
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
#include <iostream>


struct Node
{
    int data {};
    Node* next { nullptr };
};


class List {
    // MEMORY CLEAN UP NEEDED !!
public:
    List();
    void add(int n);
    void addNElements(int n);
    void display();

private:
    Node* head { nullptr };
    int n {};
    int count {};
};


List::List()
{
}


void List::addNElements(int n)
{
    std::cout << "** CREATING " << n << " NODES...\n";
    for (int i { 1 }; i <= n; ++i)
    {
        add(i);
    }
}


void List::add(int n)
{
    std::cout << "** CREATING NODE " << n << '\n';
    Node * tmp { new Node };
    tmp->data = n;
    tmp->next = head;
    head = tmp;
}


void List::display()
{
    Node* cur_node = head;
    while (cur_node != nullptr)
    {
        std::cout << cur_node->data << ' ';
        cur_node = cur_node->next;
    }
    std::cout << '\n';
}


int main()
{
    List l;
    l.addNElements(13);
    l.display();
}


Output:
** CREATING 13 NODES...
** CREATING NODE 1
** CREATING NODE 2
** CREATING NODE 3
** CREATING NODE 4
** CREATING NODE 5
** CREATING NODE 6
** CREATING NODE 7
** CREATING NODE 8
** CREATING NODE 9
** CREATING NODE 10
** CREATING NODE 11
** CREATING NODE 12
** CREATING NODE 13
13 12 11 10 9 8 7 6 5 4 3 2 1

Lastchance, yes initializing struct Node* next=nullptr did it. It is solved. THANK YOU and all who helped so quickly.
Topic archived. No new replies allowed.