LinkedList code not working

Hello. I wrote the following code:
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
#include <iostream>

class Node
{
public:
    int data;
    Node* next;
};

class List
{
public:
    List()
    {
        head = NULL;
    }
    
    void Add(int value)
    {
        Node* newNode;
        newNode->data = value;
        newNode->next = NULL;
        
        if(head == NULL) {
            head = newNode;
        }
        else
        {
            Node* temp = head;
            
            while(temp->next != NULL) {
                temp = temp->next;
            }
            temp->next = newNode;
        }
    }
	
	void Display()
	{
	    Node* temp = head;
	    while(temp != NULL)
	    {
		std::cout << temp->data << "\n";
		temp = temp->next;
	    }
	}
private:
    Node* head;
};

int main()
{
    List list;
    list.Add(10);
    list.Add(100);
    list.Display();
    return 0;
}


But I ran it and it didn't do anything. So, I decided to run it
with this website: https://www.onlinegdb.com/online_c++_compiler
and it said "Segmentation Fault" or something like that. I can't
figure out why. Please help! :P

Thanks in advance.
Last edited on
Did you use a debugger? If you haven't then learn how, it will save you hours of staring at code. That's what experienced coders do, before running down the hall yelling heeeeelp!

Have you done anything else to see what the problem is?

Have you tried rubber duck debugging?

1. Acquire a small rubber duck;
2. Verbally explain the problem to the duck, as if the duck was you boss;
3. The answer will become apparent.

Start by explaining to the duck what a linked list is.

A small thing (your problem is bigger) Don't use NULL, use nullptr instead.

Many other people have done linked list code.
I have used nullptr but I researched and could not find a difference...
Also, I don't know how to use a debugger, the one on VS2019 just
tells me why the program crashed but nothing else... I want to know
a more exact version of what the program is actually doing. Know any
good debuggers?
https://en.cppreference.com/w/cpp/language/nullptr

Also, I don't know how to use a debugger, the one on VS2019 just
tells me why the program crashed but nothing else...


The idea of a debugger is to create a watchlist of variables, step through the code 1 line at a time, keep an eye on the values, deduce where the problem is. If it segfaults, then see what is happening on the line that segfaults, work back from there. All the debuggers do this. VS debugger is apparently very good from what I have heard.

Start by explaining to the duck what a linked list is.

Many other people have done linked list code.
> Line 20: Node* newNode;

This is just an uninitialised pointer; there is no new node to which it points.
We need to create a new node first, and only after that try to use it.

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
#include <iostream>

class Node
{
    public:
        int data ;
        Node* next ;
};

class List
{
    public:

        List() noexcept = default ;

        // TO DO: destructor, copy, assignment, move

        void Add(int value)
        {
            Node* newNode = new Node{ value, nullptr } ;

            if(head == nullptr ) head = newNode;

            else
            {
                Node* temp = head;

                while( temp->next != nullptr ) temp = temp->next;

                temp->next = newNode;
            }
        }

        void Display() const noexcept
        {
            const Node* temp = head;
            while(temp != nullptr )
            {
                std::cout << temp->data << ' ' ;
                temp = temp->next;
            }

            std::cout << '\n' ;
        }

    private:
        Node* head = nullptr ;
};

int main()
{
    List list;


    for( int v : { 10, 23, 67, 99, 100, 158, 224, 337, 546, 789, 992 } )
    {
        list.Add(v);
        list.Display();
    }

}


http://coliru.stacked-crooked.com/a/dad32d0b266a6d63
Last edited on
Is it correct that Node* node = new Node; makes an object of type Node**?

Also, what is noexcept?
Last edited on
new Node; creates a new object of type Node

Node* pnode = new Node;
The result of the new expression (pointer to the created object) is used to initialise the pointer

noexcept specifies that the function would not throw any exceptions.
Last edited on
Oh, ok. Thanks. :D
Is it just me or should newNode be deleted in your code, @JLBorges?
> should newNode be deleted in your code, @JLBorges?

Should be deleted when it is no longer required; for example the destructor should delete the nodes.

The code is incomplete; // TO DO: destructor, copy, assignment, move
So what would happen if I ran it? (I did run it...)
Ok, is this code safe to run, or will it cause a memory leak?
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
#include <iostream>
#include <string>

class Node
{
public:
    int data;
    Node* next;
};

class List
{
public:
    ~List()
    {
        Node* current = head;
        while (current != 0) {
            Node* next = current->next;
            delete current;
            current = next;
        }
        head = 0;
    }

    List() noexcept = default;

    void Add(int value)
    {
        Node* newNode = new Node{ value, nullptr };

        if (head == nullptr) head = newNode;

        else
        {
            Node* temp = head;

            while (temp->next != nullptr) temp = temp->next;

            temp->next = newNode;
        }
    }

    void Display() const noexcept
    {
        const Node* temp = head;
        while (temp != nullptr)
        {
            std::cout << temp->data;
            std::cout << ' ';
            temp = temp->next;
        }
    }
private:
    Node* head = nullptr;
};

int main()
{
    List list;
    list.Add(10);
    list.Add(20);
    list.Display();
    return 0;
}
Consider:

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
#include <iostream>
#include <string>

class Node
{
public:
	int data {};
	Node* next {};
};

class List
{
public:
	~List() noexcept
	{
		while (head != nullptr) {
			const auto next {head->next};

			delete head;
			head = next;
		}
	}

	List() noexcept = default;

	void Add(int value)
	{
		const auto newNode {new Node {value, nullptr}};

		if (head == nullptr)
			head = newNode;
		else
		{
			auto temp {head};

			for (; temp->next != nullptr; temp = temp->next);
			temp->next = newNode;
		}
	}

	void Display() const noexcept
	{
		for (auto temp {head}; temp != nullptr; temp = temp->next)
			std::cout << temp->data << ' ';

		std::cout << '\n';
	}

private:
	Node* head {};
};

int main()
{
	List list;

	list.Add(10);
	list.Add(20);

	list.Display();
}

Topic archived. No new replies allowed.