-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;
}
I understood yes, until sanou another doubt that I had. However is only being printed the variable "sizeLista". I did something wrong?
– FernandoHCA
Have you changed any other part of the code? Here has it the same way it was posted and is working.
– Andre
Now it worked! I copied the code again and made the suggested changes and it worked. Thank you so much for your help!
– FernandoHCA