Threaded Dynamic List Creation

0

I am studying data structure through the site: Uncomplicated C Language - Data Structure and in class 12 (3:52 min) the teacher develops the function that creates the list:

// Implementação das funções
lista* criarLista(){
    lista* lde = (lista*) malloc(sizeof(lista));

    if(lde != NULL)
        *lde = NULL; // inicializa o ponteiro lde com NULL (conteúdo).

    return lde;
}

The fact is that I did not understand the assignment made in *lde = NULL , since the teacher comments that *lde is the pointer to the head of the list and that head points to the next node that is NULL. So I expected something like *lde->prox = NULL . The teacher works with the modularization of TADs, and then divides the code between the public part - header (lde.h):

// Definição do tipo de dado a ser armazenado na lista
struct Aluno{
    float n1,
          n2,
          n3,
          n4;
    int matricula;
    char nome[50];
};

// Definição do ponteiro do tipo lista
typedef struct Elemento* lista;

/*Protótipos*/
// Funções básicas
lista* criarLista();

and the private part - implementation (lde.c):

#include<stdio.h>
#include<stdlib.h>
#include<conio2.h>
#include "LDE.h"

// Definição do tipo de dado "lista"
struct Elemento{
    struct Aluno aluno;
    struct Elemento *proximo; // Apontador para a próxima estrutura do tipo struct (porque os elementos são estruturas)
};

// Apelidando a lista para facilitar a manipulação
typedef struct Elemento elmt;


// Implementação das funções
lista* criarLista(){
    lista* lde = (lista*) malloc(sizeof(lista));

    if(lde != NULL)
        *lde = NULL; // inicializa o ponteiro lde com NULL (conteúdo).

    return lde;
}

and the client / application program (test.c):

#include<stdio.h>
#include<stdlib.h>
#include<conio2.h>
#include "LDE.h"

struct Aluno estudante;

int main(){
    int opcao;
    lista* lst = NULL; 
    lst = criarLista();
}
    
asked by anonymous 19.11.2018 / 17:11

1 answer

0

The question here refers to the classic typedef pointer you want to help but often ends up by visually deceiving:

typedef struct Elemento* lista;

So it may not be very obvious but lista is a pointer to a struct Elemento . Just when it does:

lista* lde = (lista*) malloc(sizeof(lista));

You are creating a pointer to an element of type lista , that is a double pointer of struct Elemento . So it would be equivalent to having done:

struct Elemento** lde = (struct Elemento**) malloc(sizeof(struct Elemento*));

Each node is allocated and the pointer is saved to it, and when it wants to have something that points to a node then it has a pointer pointer. In case when doing:

if(lde != NULL) //se conseguiu alocar memoria
    *lde = NULL; //coloque o ponteiro de nós a apontar para NULL

You are saying: if you could allocate memory, put the node pointer pointing to nothing, represented by NULL .

Let's visually represent the three alternatives versus lde :

  • lde = NULL :

      lde
    _______
    | NULL |
    --------
    

    lde "does not point to any address", or points to the null address

  • *lde = NULL :

      lde
    _______
    |_____|  ----> NULL
    

    lde points to a valid address but at this address has NULL .

  • lde = (lista*) malloc(sizeof(lista))

      lde           elemento*
    _______        _________       _________
    |_____|  ----> |_______| ----> | Aluno | 
                                   ---------
                                   | prox  | -------> NULL
                                   ---------
    

    Now lde points to a valid, allocated node. It is clear that pointer pointer as it has two arrows. The elemento* corresponds to the pointer of a node, which in the previous diagram was NULL .

19.11.2018 / 18:19