Dynamic Chained List Creation

Asked

Viewed 101 times

0

I am studying data structure by 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 professor comments that *lde is the pointer to the head of the list and that head points to the next node which is NULL. Soon, I expected something like *lde->prox = NULL. The teacher works with modularization of Tads, 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();
}

1 answer

0


The question here refers to the classic typedef pointer that aims to help but often ends up visually deluding:

typedef struct Elemento* lista;

So it may not be very clear 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 a type element lista, or 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 kept for it, and when you want to have something that points to a node then has a pointer pointer. In the case when doing:

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

You’re saying, if you were able to allocate memory, put the node pointer to nothing, represented by NULL.

Let’s visually represent the three alternatives to 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 NULL.

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

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

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

Browser other questions tagged

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