Print threaded list in C

1

I'm creating a program that receives a vector and creates a linked list. I need help to create the "print ()" function, which prints the generated threaded list. I do not know how to proceed.

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

// elemento da lista
typedef struct estr {

        int valor;

        struct estr *prox;

} NO;

NO* insereNoNaLista(NO* p, NO* lista){
    if(!lista) return p; //Se não houver nada na caixa de nós(lista = NULL), o ponteiro p aponta para NULL
    while(lista->prox != NULL ){ //"lista->prox = NULL" eh o ultimo elemento da lista
        lista = lista->prox; //Percorre os nós até que próximo elemento seja NULL
    }
    lista->prox = p; //Se lista->prox não for NULL, então o ponteiro p guarda o endereço de memoria do proximo elemento
    return lista; //devolve lista com todos os nos
}

//Recebe um vetor de inteiros e devolve uma lista ligada de nos
NO* deVetorParaLista(int *v, int t){
    int i;
    NO* p = NULL; 
    NO* lista = NULL; 

    for(i = 0; i < t; i++ ){
        p = malloc(sizeof(NO)); 
        p->valor = v[i]; 
        p->prox = NULL;
        lista = insereNoNaLista(p, lista); 
    }
    return lista;
}

void imprimir()

int main() {

    int v[] = {1,53,43,68,99, -7};
    int t = (sizeof(v))/sizeof(int); //tamanho do vetor v
    deVetorParaLista(v, t);

}

Another question: the insertNoNaList and functions of ListView return "list". In what way could I merge these two functions into one or use best programming practices?

    
asked by anonymous 03.09.2016 / 18:53

1 answer

5

A good programming practice would be to use more explanatory names for variables. In a small program like this it does not matter, but in more complex programs it is better to use names that minimize the purpose of the variable, especially if it is something more than a simple counter.

Another is to always free memory when allocating dynamically. In general for each malloc there should be a free. Again it would not be a problem in a small, non-repetitive program at the discretion of the user, and it does not use much memory as is the case as the kernel releases the memory as soon as the process is terminated. But it's always good practice :).

I took the liberty to follow your suggestions and make a merged version of the first two functions and add a function to clean memory.

Good luck in your studies.

// elemento da lista
typedef struct estr 
{

    int valor;
    struct estr *prox;

} NO;
//Recebe um vetor de inteiros e devolve uma lista ligada de nos
NO* deVetorParaLista(int *v, int t)
{
    int i;
    NO* p = NULL;
    NO* a = NULL;//endereco do no anterior
    NO* lista = NULL;

    for(i = 0; i < t; i++ )
    {
        p = (NO*)malloc(sizeof(NO));
        p->valor = v[i];
        p->prox = NULL;

        if(i==0)
        {
            //copia primeiro endereco para variavel a ser retornada
            lista=p;
        }else{
            //se nao for o primeiro elemento copia endereco para 'prox' do no anterior
            a->prox=p;
        }
        a=p;
    }
    return lista;
}

void imprimir(NO *p)
{
    printf("\n");
    while(p!=NULL)
    {
        printf("%d",p->valor);
        printf("\n");
        p=p->prox;
    }
}
//libera memoria alocada
void limparLista(NO *p)
{
    NO *n;
    while(p!=NULL)
    {
        n=p->prox;
        free(p);
        p=n;
    }
}

int main()
{

    int v[] = {5,17,-2,55,1000};
    int t = (sizeof(v))/sizeof(int); //tamanho do vetor v
    NO* p=deVetorParaLista(v, t);

    imprimir(p);

    //limpar lista(somente depois de usa-la)
    limparLista(p);

    return 0;
}
    
04.09.2016 / 02:53