Transforming a Single List into a Double Chained List

Asked

Viewed 1,028 times

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;   
}
  • 1

    What is your logic for implementing the condition if((aux->info%2) == 0) in function modifica? And I think you’re kind of getting lost in inserting the knots into ant and prox. Do the table test and check how it is currently.

1 answer

0


There are only 2 corrections to be made:

1) The notes are reversed

LD* insereLD(LD* l, int valor){
    LD* novo = (LD*)malloc(sizeof(LD));
    novo->info = valor;
    /* ant e prox estão invertidos
    novo->ant = l;
    novo->prox = NULL;
    */
    novo->prox = l;
    novo->ant = NULL;
    if(l != NULL)
        l->ant = novo;
    return novo;
}

2) You are creating an LD without info, so you will create a position with the info set to a memory junk. Starting LD with NULL only solves this problem

LD* modifica(Lista* l){
    /* Ira imprimir um lixo de memoria
    LD* novo = (LD*)malloc(sizeof(LD));
    novo->ant = NULL,
    novo->prox = NULL;
    */
    LD* novo = NULL;

    Lista* aux = l;
    while(aux != NULL){
        if((aux->info%2) == 0){
            novo = insereLD(novo, aux->info);
        }
        aux = aux->prox;
    }
    return novo;
}

The rest of the code is correct.

Browser other questions tagged

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