1
I’m creating a program that receives a vector and creates a chained list. I need help creating the "print()" function, which prints the generated chained list. I don’t 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 functions inserts and fromVetorParaList return "list". How I could merge these two functions into a single one or utilize best programming practices?
You have to go through the Prox and print 'printf' the values you want
– Vedovotto