1
For a college job, I need to get a C function to receive a Single List and return a Double Chained List with the even values of the Single List that was passed. My attempt is this, but only displays the value '2'.
typedef struct lista{
int info;
struct lista* prox;
}Lista;
typedef struct listaDupla{
int info;
struct listaDupla* ant;
struct listaDupla* prox;
}LD;
Lista* insereLista(Lista* l, int valor){
Lista* novo = (Lista*)malloc(sizeof(Lista));
novo->info = valor;
novo->prox = l;
return novo;
}
void imprimeLista(Lista* l){
printf("---- lista -----\n");
do{
printf("%d\n", l->info);
l= l->prox;
}while(l != NULL);
}
LD* insereLD(LD* l, int valor){
LD* novo = (LD*)malloc(sizeof(LD));
novo->info = valor;
novo->ant = l;
novo->prox = NULL;
if(l != NULL)
l->ant = novo;
return novo;
}
LD* modifica(Lista* l){
LD* novo = (LD*)malloc(sizeof(LD));
novo->ant = NULL,
novo->prox = NULL;
Lista* aux = l;
while(aux != NULL){
if((aux->info%2) == 0){
novo = insereLD(novo, aux->info);
}
aux = aux->prox;
}
return novo;
}
void imprimeLD(LD* ListaDupla) {
printf("\n---- lista dupla ----\n");
while(ListaDupla != NULL){
printf("%d\n", ListaDupla->info);
ListaDupla = ListaDupla->prox;
}
}
int main(){
Lista* list = NULL;
LD* listDup = NULL;
list = insereLista(list, 1);
list = insereLista(list, 2);
list = insereLista(list, 4);
list = insereLista(list, 89);
list = insereLista(list, 88);
imprimeLista(list);
listDup = modifica(list);
imprimeLD(listDup);
return 0;
}
What is your logic for implementing the condition
if((aux->info%2) == 0)
in functionmodifica
? And I think you’re kind of getting lost in inserting the knots intoant
andprox
. Do the table test and check how it is currently.– Woss