How do I print the data into a dynamically double-chained list in C?

Asked

Viewed 342 times

-2

Good afternoon, folks. This is my first question here on the site, so suggestions are welcome. I’m writing a code using Doubly Chained Dynamic List for the college’s Data Structure class and I’m having a hard time printing the list to see if the values have been assigned correctly. Most of the examples I find on the Internet are done differently to what my teacher asks, could you help me? I’ve tried variations on the function, though: either the compiler hangs or only the last inserted value is printed. Follow the code written in C in Codeblocks:

#include <stdio.h>
#include <stdlib.h>
struct elemento {
    int info;
    struct elemento *prox;
    struct elemento *ant;
};
typedef struct elemento tipoElemento;

struct estruturaLDDE{
    tipoElemento *primeiro;
    tipoElemento *ultimo;
    int tamanhoLista;
};
typedef struct estruturaLDDE tipoLDDE;

void inicializaLista (tipoLDDE *listaAux){
    listaAux->primeiro = NULL;
    listaAux->ultimo = NULL;
    listaAux->tamanhoLista=0;
}
void insereElementoFinal (tipoLDDE *listaAux){
    tipoElemento *novo = (tipoElemento*) malloc(sizeof(tipoElemento));
    int n, m;
    for(n=0;n<10;n++){
        printf("digite o numero: ");
        scanf("%d", &novo->info);
        if (listaAux->tamanhoLista == 0){
            novo->prox = NULL;
            novo->ant = NULL;
            listaAux->primeiro=novo;
            listaAux->ultimo=novo;
        }
        else{
            novo->prox = NULL;
            novo->ant = listaAux->ultimo;
            listaAux->ultimo->prox = novo;
            listaAux->ultimo=novo;
        }
        listaAux->tamanhoLista++;
    }
}
/*void libera(tipoLDDE *eli){
    tipoLDDE *no = eli, *aux;
    while (no != NULL){
        aux = no;
        no->ultimo = no->ultimo->prox;
        free (aux);
    }
    eli = NULL;
}*/
void imprime(tipoLDDE *b){
    tipoLDDE *a;
    a->ultimo=b->ultimo->prox;
    int i;
    if(a->ultimo == NULL) printf("a lista esta vazia");
    else{
        while(a->ultimo != NULL){
           printf("%i\n", a->ultimo->info);
           a->ultimo=a->ultimo->prox;
        }
    }
    printf("%i", a->tamanhoLista);
}

int main(){
    tipoLDDE *li= (tipoLDDE*) malloc(sizeof(tipoLDDE));
    printf("-------BEM VINDO AO JOGO DE SOMA 12-------\n");
    inicializaLista(li);
    insereElementoFinal(li);
    imprime(li);
   // libera(li);
    return 0;
}



1 answer

0


Fernando, your first mistake is in the function insereElementoFinal. You’re allocating memory space to the node only once. You need to allocate once for each value you will store, so instead of initializing novo at the beginning of the function, within the for you should allocate a new memory space and assign the position of that space to novo at each iteration.

void insereElementoFinal(tipoLDDE *listaAux) {
    int n, m;
    tipoElemento *novo;

    for (n = 0; n < 10; n++) {
        novo = (tipoElemento*) malloc(sizeof(tipoElemento));
        printf("digite o numero: ");
        scanf("%d", &novo->info);

        if (listaAux->tamanhoLista == 0) {
            novo->prox = NULL;
            novo->ant = NULL;
            listaAux->primeiro=novo;
            listaAux->ultimo=novo;
        } else {
            novo->prox = NULL;
            novo->ant = listaAux->ultimo;
            listaAux->ultimo->prox = novo;
            listaAux->ultimo = novo;
        }

        listaAux->tamanhoLista++;
    }
}

Now to go through the list, I didn’t quite understand what your idea was with the code posted. You just needed to create an auxiliary variable with the same type as your node, and reassign it to the next value until the next value is NULL, that is, until the end of the list.

void imprime(tipoLDDE *b) {
    tipoElemento *aux = b->primeiro;

    if (aux == NULL) {
        printf("A lista esta vazia");
    } 

    while (aux != NULL) {
        printf("%i\n", aux->info);
        aux = aux->prox;
    }

    printf("Tamanho da lista: %i", b->tamanhoLista);
}

I could understand the logic?

  • I understood yes, until sanou another doubt that I had. However is only being printed the variable "sizeLista". I did something wrong?

  • Have you changed any other part of the code? Here has it the same way it was posted and is working.

  • Now it worked! I copied the code again and made the suggested changes and it worked. Thank you so much for your help!

Browser other questions tagged

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