1

Sheepishly I must admit I am having difficulty understanding how Linked Lists are implemented. Spent an hour and a half just on this part. The following partial code is from the lecture (comments removed):

typedef struct node
{
    int number;
    struct node *next;
}
node;

int main(void)
{

    node *list = NULL;
    node *n = malloc(sizeof(node));

    if (n == NULL)
    {
        return 1;
    }

    n->number = 1;
    n->next = NULL;

    // Add node n by pointing list to it, since we only have one node so far
    list = n;
}

My understanding is that list is a pointer variable initialised without a value, and n is also a pointer variable. They both have the number and *next "components" of a node.

Therefore, list = n basically makes list become n. I get this much.

What I fail to grasp is: list->next = n, partial (continued from above) code for reference:

 n = malloc(sizeof(node));
    if (n == NULL)
    {
        free(list);
        return 1;
    }

    // Set the values in our new node
    n->number = 2;
    n->next = NULL;

    // Update the pointer in our first node to point to the second node
    list->next = n;

list->next = n can be expressed also as (*list).next = n. I verbalise this as:

"go to the address of list, access the part of memory that stores next, and assign to it n".

How is it possible to store both the number component and next component of n, in just the next component of list?

How the foobar do you even fit them in memory wise?

0

My understanding is that list is a pointer variable initialised without a value

No. It's initialized to NULL. That's what the = NULL part does. A variable can't be initialized without a value. Initializing means assigning a value. Uninitialized meaning memory is allocated, but you didn't write anything to it. Maybe this is what you meant, a pointer being NULL tends to mean it's "empty" so to speak.

n is also a pointer variable. They both have the number and *next "components" of a node.

n is a pointer, that means it's holding a memory address. Neither list nor n have number and next. At least not directly.

Therefore, list = n basically makes list become n.

Kinda. It means you're copying the contents of n and writing it to list. So list and n now point to the same location in memory.

What I fail to grasp is: list->next = n

That doesn't really make sense in the context of the earlier code, if list = n; then adding list->next = n; would cause the data structure to loop around on itself.

Edit: Scratch that last part. I was confusing the two code snippets. It doesn't loop around on itself.

list->next = n can be expressed also as (*list).next = n. I verbalise this as: "go to the address of list, access the part of memory that stores next, and assign to it n"

Almost. It means, go to the node that list is pointing to. The rest of it looks correct.

How is it possible to store both the number component and next component of n, in just the next component of list?

Well you don't. next is also a pointer. So it too is holding the memory address of a node. You have to allocate more space with malloc before you can do anything more with it.

Normally you have a push function that adds items to a linked list. They tend to look like this:

void push(node **list, int item) {
    node *n = malloc(sizeof(node));
    n->item = item;
    n->next = *list;
    *list = n;
}

Assuming you have node *list = NULL; you can add numbers to the list like this: push(&list, 5);

As you can see, you have to call malloc every time you want to add another node to the list.

This answer got a bit messy. I'm sorry about that. Do let me know if I can clear anything up.

4
  • Hey there, thanks for the answer. I think what I overlooked was how "next" was assigned to NULL. Somehow I had the impression that "next" was already allocated memory. – Orangecat Apr 22 at 1:17
  • 1
    Actually, I wanted to ask why "list->next=n would cause the data structure to loop around on itself". "list = n" makes them both point to the same location, which contains a value and a pointer. In the code above, n is malloc-ed again, and assigned a new value and a NULL pointer. I'm under the impression that there would be no loop since "list->next=n" assigns that new value and NULL pointer to "next", am I missing something? – Orangecat Apr 22 at 1:37
  • @Orangecat Ah, I'm sorry. I was confusing your two code snippets. You're right, assuming the the second snippet follows the first, it won't create a loop. – Fuelled_By_Coffee Apr 22 at 6:19
  • Alright, thanks for the clarification! I think I recognise your username from reddit, you've helped quite a few people, kudos to you! – Orangecat Apr 22 at 7:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .