0

In pset 5s walkthrough, the node struct is defined like this:

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

And when we then use the cursor -> next statement, to jump to the next node. How does the struct exactly know to jump to the next, what connects them?

I understand that the cursor points to the "current node" and then -> next will go to the next node in the linked list.

When defining the node struct, is next a predefined keyword?

As far as my understanding of pointers, we are creating a pointer to the struct node and calling it "next", but I don't see how the nodes are connected.

Thanks

1
0

next is just a variable inside a structure of type node. next's type, is a pointer to a structure of type node. Try to get your head around this. Each structure node, has a variable next inside it, that is a pointer to another structure `node.

So when you start with a pointer to the head of the linked list, if you want to get to the next node in the list, you just have to follow the pointer inside your current structure.

// initialize cursor to the head of the linked list
node *cursor = head;

// cursor get's advanced to the next node, by following the pointer inside the
// structure it's currently pointing at
cursor = cursor->next;

It's explained pretty well in the lectures, you should watch them.

1
  • 1
    I may have phrased my question poorly, but it's the advancing of the next node that was unclear to me, but it has been explained to me now. Thank you :) – Jake Jul 17 '17 at 7:50

You must log in to answer this question.

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