In a chained list structure, why is a pointer to the node pointer used?

Asked

Viewed 60 times

3

When reading my teacher’s slides I was left with a question regarding this structure:

struct node
{
    char item;
    struct node *next;
};
typedef struct node Node;
typedef node *Lista;  

It wasn’t clear to me why typedef node *Lista, because I thought there was already a pointer to the next struct. Could someone explain to me why?

1 answer

1


Lista is a type to organize the list, it is not the next structure, it is the first, it is where it starts the list. If every node is accessed via a pointer, how is the first node accessed? By an external pointer. That’s it. There’s not really a pointer to pointer, there’s only one pointer to a structure, which inside has another pointer.

  • So Lista would be a pointer previously created to access the first element? Why couldn’t I do this just by making a Node->next?

  • That would be it. You can’t do direct access because you don’t have an item when you start. In fact, if you want to do it is you who has to say how you will, I don’t see how you could.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.