0

Well, I am trying to write a linked list (a very simple linked list) with very few code. This is my code:

#include <iostream>

using namespace std;

class node
{
public :

    int data = 0;

    node* next;
};

node node_obj;

int main()
{
    node* head;
    node* node_pointer = head;
    node_pointer = node_obj.next;
    node_pointer->data = 4;
    node_pointer = node_obj.next;
    node_pointer->data = 5;
    node_pointer = node_obj.next;
    node_pointer->data = 6;
    node_pointer = node_obj.next;

    return 0;
}

I am trying just to make it work at first, that's why there are no add, print or remove functions.

Anyway every time I run the program it crashes and I get an error code C0000005.

As far as I know this is an error that indicates memory access violation but I can't find the solution. Update: I changed my code and now it looks like this(included only the changed part):

class node
{
public :
    node();
    int data = 0;
    node* next;
};
node::node()
{
    next = new node;
}

So now i have an initialized ' new' pointer and the error code changed to c00000fd

2 Answers2

1

First, you should use a debugger before asking such questions. And the problem(s) in you code are:

node node_obj;

int main()
{
    node* head;
    node* node_pointer = head; // Warning: dead storage. You never use the value you initialized here.
    node_pointer = node_obj.next; // node_obj.next was never initialized, so now node_pointer points nowhere.
    node_pointer->data = 4; // And here you use nowhere-pointing pointer to 
    // access structure member. According to the Standard,
    // it's [undefined behavior][1] which in practice usually leads to application crash.
    // All subsequent lines suffer from the same issue.
    node_pointer = node_obj.next;
    node_pointer->data = 5;
    node_pointer = node_obj.next;
    node_pointer->data = 6;
    node_pointer = node_obj.next;

    return 0;
}

Probably you wanted to write something like this:

node *head;

int main()
{
    head = new node;
    node *node_pointer = head;
    node_pointer->data = 4;
    node_pointer->next = new node;
    node_pointer = node_pointer->next;
    node_pointer->data = 5;
    node_pointer->next = new node;
    node_pointer = node_pointer->next;
    node_pointer->data = 6;
    node_pointer->next = nullptr; // List end marker.
    return 0;
}
Sergey
  • 7,985
  • 4
  • 48
  • 80
0

The first error you'll get, is when you try to use node::node(). If you look at your code:

...
node::node()
{
    next = new node;
}
...

node::node() is a constructor of a class. A constructor of a class is a function that will be executed when you create an instance of that class. Let's say you create a new node:

node n; 

node::node() is called. But hey, what is it doing?

next = new node;

It's creating a new node... which means calling node::node() again, and again ad infinitum. This is why your program crashed.

Back to the linked list

If you want to write a linked list, you don't just need nodes. Ideally, you want to encapsulate all the nodes such that it's hidden from anyone who is using your linked list library. I won't go into all the text book information, but briefly:

  1. It makes sure that no one can accidentally modify your nodes (makes sure that they behave in an intended way)
  2. Less code on the screen. Your users won't get overwhelmed with all the little details. (All the linked list code can be kept in another file, and the user can just focus on what s/he needs to do. )

Here's an example implementation.

#include <iostream>

// Declaration can be put into LinkedList.h
struct node {
    node* next_node;
    char* some_data;

    node()
    {
        next_node = nullptr;
    }
};

class LinkedList
{
    node* head;
    node* tail;

public:
    LinkedList();
    ~LinkedList() { /* if you do this properly, you need to clear up all the nodes you have created... */}
    void AddNode(char* data);
    void PrintNodeData();
};


// Method implementation (The details) can be put into LinkedList.cpp


LinkedList::LinkedList()
{
    head = nullptr;
}

void LinkedList::AddNode(char* data)
{
    node* new_node = new node();
    new_node->some_data = data;

    if (head == nullptr)
    {
        // initialise the first node
        head = new_node;
    }
    else
    {
        node*n = head;
        while (n->next_node != nullptr)
        {
            // move on to the next node
            n = n->next_node;
        }
        n->next_node = new_node;
    }
}
void LinkedList::PrintNodeData()
{
    // Loop through all the nodes 
    for (node*n = head; n != nullptr; n = n->next_node)
    {
        std::cout << "Data: " << n->some_data << std::endl;
    }
}

// this is all the code the user has to pay attention to
int main()
{
    LinkedList ll;
    ll.PrintNodeData(); // prints nothing, since head == nullptr at this point
    ll.AddNode("Data for my 1st node.");
    ll.AddNode("Data for my 2nd node.");
    ll.AddNode("Data for my 3rd node.");
    ll.AddNode("Data for my 4th node.");
    ll.PrintNodeData(); // prints all the data
    return 0;
}

Hope this helps, and I hope you enjoy your journey of self-discovery. :-)

bot1131357
  • 877
  • 8
  • 25