What is the meaning of Node?

4

I'm studying piles and queues, below I was given a code to parse. The code first creates queue after queuing the values. After that it removes each of the values from the queue and tells you whether or not you succeeded in removing the value.

What I did not understand was the meaning of a node, whether it's a struct that stores pointer or something.

Code:

#include<stdio.h>
#include<stdlib.h>

typedef struct nodo 
{
    float dado;
    struct nodo *proximo;
} Nodo;


int push(Nodo **inicio, float dado) {
    Nodo *nodo;
    if ((nodo = (Nodo *) malloc(sizeof(Nodo)))  == NULL) 
      return 0;

    nodo->dado    = dado;
    nodo->proximo = NULL;  

    if (*inicio != NULL) 
        nodo->proximo = *inicio;

    *inicio = nodo;
    return 1;   
}

int pop(Nodo **inicio, float *dado) {
    if (*inicio == NULL) 
        return 0;
    else {
        Nodo *auxiliar = *inicio;
        *dado = (*inicio)->dado;
        *inicio = (*inicio)->proximo;
        free(auxiliar);
    }
    return 1;       
}

void DestroiLista(Nodo **inicio) {
    Nodo *ptrAux;
    while (*inicio != NULL) {   
        ptrAux = *inicio;
        *inicio = ptrAux->proximo;
        printf("Destruindo %.1f\n",ptrAux->dado);       
        free(ptrAux);
    }
}

int main( ) {
    int i;
    float dado;
    Nodo *pilha = NULL; 

    push(&pilha, 5);
    push(&pilha, 2);
    push(&pilha, 7);
    push(&pilha, 6);
    for (i=0;i<6;i++) {
        if (pop(&pilha,&dado)) 
            printf("Deu certo remover o valor %f da fila\n",dado);
        else
            printf("Nao deu certo remover um elemento da pilha\n");
    }
    DestroiLista(&pilha);
    return 0;
}
    
asked by anonymous 15.12.2017 / 20:32

1 answer

4

This looks like an implementation of a linked list . It stores the reference to the next item, if it does not exist, then null .

  

is a linear collection of data elements, whose order is given with each element pointing to next

In the image, each block of two houses is a node and the last house in each block is the reference to the next node . In this example, I can represent this:

nodo1.valor   = 12
nodo1.proximo = &nodo2

nodo2.valor   = 99
nodo2.proximo = &nodo3

nodo3.valor   = 37
nodo3.proximo = null
  

The & operator was placed only to emphasize that the value to be stored in the nodo.proximo attribute is the address of the desired node

    
15.12.2017 / 20:40