Doubt - Dynamic List

1

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;

For this structure, I have two methods:

Create List Method:

List *CreateList ()

{

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

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

    return list;

}

On the list->head = NULL line, will all elements inside the *head (Node type) pointer receive the NULL value? The id variable and the *Next pointer will be 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++;   

}

Why does the pointer of the next node receive the address of the node itself ( node->Next = list->head; )? In the next line the node receives the address of the *node pointer (type Node) ( list->head = node; ). When it creates the *node pointer, is it creating the node in the list? And when the *head pointer gets the address of the *node pointer, is it receiving the node address?

Thank you.

    
asked by anonymous 11.12.2018 / 02:04

1 answer

0

In your List *CreateList () function you only allocate a List structure containing int and a pointer to Node , which is head , and nothing else. And after that you make your pointer point to NULL , so in there it does not have any structure that actually represents Node .

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

The following is 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 is being placed at the top 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++;   

}

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

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

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

    
11.12.2018 / 05:11