What does Nodo mean?

Asked

Viewed 708 times

4

I am studying stacks and queues, below I was given a code to analyze. The code first creates queue then queues the values. After this, it removes each of the values from the queue and warns whether or not it was successful by removing the value.

What I didn’t understand was the meaning of the node, whether it’s a pointer-storing struct 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;
}

1 answer

4


This looks like an implementation of a chained list (Linked list). It stores the reference for the next item, if it doesn’t exist, then null.

is a linear collection of data elements, the order of which occurs with each element pointing to the next

In the image, each two-house block is a node and the last house of each block is the reference to the next node. In this example, I can act like 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 attribute nodo.proximo is the desired node address

Browser other questions tagged

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