1

I've been searching around for a good while now on google and a few text books and I can't seem to understand why it is, when building a linked list, that the nodes need to be pointers.

Eg. If i have a node defined as:

typedef struct Node{
    int value;
    struct Node *next;
} Node;

why is it that in order to create a linked list, I would say:

Node *a = malloc(sizeof(Node));
Node *b = malloc(sizeof(Node));
a->value = 1;
b->value = 2;

a->next = b;
b->next = NULL;

rather than:

Node a, b;
a.value = 1;
b.value = 2;

a.next = &b;
b.next = NULL;

To my understanding, the list will still be able to be referenced and traversed as normal, the only difference is the use of the dot, ampersand syntax rather than the arrow?

2
  • 2
    Your Node *a, *b snippet is invalild. You're declaring pointer variables, without having them point to valid memory. The pointers almost certainly won't point to valid Node objects Commented Oct 30, 2016 at 16:34
  • With your second example, think about how you would add hundreds of new nodes to your list. Using pointers, you can keep using malloc to add new nodes inside a loop. Without pointers, then you're need to allocate storage some other way, somewhat defeating the benefit of using a linked list. Commented Oct 30, 2016 at 18:04

2 Answers 2

4

You can create the list in a way that you mentioned.

But you must care for the life time of the list members. If your

Node a, b;

are in scope of a function then these are lost after the return of that function.

When you use pointers then you usually use the heap and the instances live until they are deleted.

Sign up to request clarification or add additional context in comments.

Comments

0

Your first example will not work. You are declaring two Node pointers. However, since you do not initialize these to anything, it is illegal to deference them, since they don't point to anything. You must first use something like malloc to declare memory for them to point to, or assign them to a previously declared local variable. However, you must also remember to call free when you are done with the memory.

In the second example, you are declaring two Node variables, which you use to store instances of the Node. These will be allocated on the stack if they are local variables, and they will live for as long as they are in scope. They hold valid memory, and thus you can use them in the way you have demonstrated.

1 Comment

My mistake. But under the assumption that i have allocated the required space for a and b, say if i have my two malloc statements underneath "Node *a, *b" for example.. what would be the impending differences at that point between that example of a linked list and the second one that i mentioned?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.