Doubt - Dynamic list

Asked

Viewed 84 times

2

I have the following structure for my dynamic list:

typedef struct DataNode

{

    int id;

} DataNode;


typedef struct Node

{

    DataNode data;
    struct Node *Next;

} Node;

typedef struct List 

{

    int size;
    Node *head;

} List;

inserir a descrição da imagem aqui

For this structure, I have two methods:

Method Create List:

List *CreateList ()

{

    List *list = (List*) malloc(sizeof(List));

    list->size = 0;
    list->head = NULL;

    return list;

}

On the line list->head = NULL, all elements inside the pointer *head (type Node), will receive the NULL value? The variable id and the pointer *Next will receive the value NULL?

Push method:

void Push (List *list, DataNode data01)

{


Node *node = (Node*) malloc (sizeof(Node));

node->data = data01;
node->Next = list->head;
list->head = node;
list->size++;   

}

Because the pointer of the next node receives the address of the node itself ( node->Next = list->head; )? In the next line the node receives the pointer address *node (node-type) ( list->head = node; ). When he creates the pointer *node, he is creating the node in the list? And when the pointer *head receives the address of the pointer *node, he’s getting the node address?

Thank you.

1 answer

1


In his job List *CreateList () you allocate only one structure List containing a int and a pointer to a Node, which is your head, and nothing else. And after that you make your pointer point to NULL, therefore, there is no structure inside that represents a Node in fact.

So when you create the list, you have nothing but a structure with a int and an empty pointer to a Node.

This is what’s happening:

List *CreateList ()

{

    List *list = (List*) malloc(sizeof(List));

    list->size = 0;

    //Aqui você está fazendo a variável head da estrutura recém alocada list
    //Aponta para NULL, já que você não alocou nenhuma estrutura para ela 
    //apontar. Então, dentro do head não tem nenhuma estrutura Node de fato
    list->head = NULL;

    return list;

}

In function void Push the new Node allocated ta being positioned at the beginning of the list:

void Push (List *list, DataNode data01)

{

    Node *node = (Node*) malloc (sizeof(Node));

    node->data = data01;

    //Aqui você está fazendo com que o ponteiro que está definido dentro da estrutura
    //Node, aponte para um endereço de memória, que é o endereço apontado 
    //pela variável head da estrutura lista
    node->Next = list->head;

    //Aqui você está fazendo o ponteiro head da estrutura List apontar para o novo
    //Node alocado, que vai ser o novo head
    list->head = node;
    list->size++;   

}

So when your list is empty and the function Push() to be called for the first time node->Next will equal NULL, since the pointer list->head points to NULL.

So, to make it easier to visualize, you’re pushing all the elements of the list back and putting a new element at the beginning.

Remembering that a pointer is a variable that you declare somewhere in your code that will point to a memory address.

  • I think I got a little bit better. Thank you.

Browser other questions tagged

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