Remove an element previous to the element specified by the user in a List. C

Asked

Viewed 102 times

0

I have to create a program, which will create a dynamically double-chained list and this program should do the following:

the program will read values that the user writes and add to the list. Then the user will enter a value. The program should look for that value in the list or delete the element before it.

For example:

In the list with the elements {1, 2, 3, 4}, if the user enters code 3, element 2 must be deleted. I know that in this case, for example, I should connect the "Prox" pointer from 1 to 3 and the "ant" pointer from 3 to 1, but I’m not getting it.

If the user enters the first element or one that is not in the list an error message will appear (this part has already implemented).

The problem is that I am not being able to delete the previous item from the list. When I set to perform this function and then print the list, the element is still there.

I used the following structures:

typedef struct codigoProduto{
    int cod;
    struct codigoProduto *ant;
    struct codigoProduto *prox;
} produto;

typedef struct Lista{
    int tam;
    produto *inicio;
    produto *fim;
} lista;

And I created the function to remove the previous element that was left as follows:

void retiraAnterior(lista* LISTA, int indice){
    produto* tmp = LISTA->inicio;
    int vazio, achou;

    vazio = listaVazia(LISTA);
    if (vazio == 0){
        achou = busca(LISTA, indice);
        if (!achou){
            printf("Nao existe elemento solicitado na lista.\n Impossivel remover anterior.\n");
        }
        else{
            if (LISTA->inicio->cod == indice){
                printf("O elemento solicitado eh o primeiro da lista.\nImpossivel remover anterior.\n");
                return menu();
            }
            while(tmp->cod != indice){
                if (tmp->cod == indice){
                    tmp->ant->ant->prox = tmp;
                    tmp->ant = tmp->ant->ant;
                    LISTA->tam--;
                }
                tmp = tmp->prox;
            }
        }
    }
}

The empty function returns 1 if the list is empty and 0 otherwise. While the search function returns 1 if you find the element in the list and 0 otherwise.

1 answer

0

I managed to resolve by making the following changes:

    void retiraAnterior(lista* LISTA, int indice){
        produto* tmp = LISTA->inicio;
        int vazio, achou;

        vazio = listaVazia(LISTA);
        if (vazio == 0){
            achou = busca(LISTA, indice);
            if (!achou){
                printf("Nao existe elemento solicitado na lista.\n Impossivel remover anterior.\n");
            }
            else{
                if (LISTA->inicio->cod == indice){
                    printf("O elemento solicitado eh o primeiro da lista.\nImpossivel remover anterior.\n");
                    return menu();
                }

                while(tmp->cod != indice){
                    tmp = tmp->prox;
                    //aux = tmp->ant;
                }
//tirei o if do loop e criei outra condição de borda pois estava dando erro sem ela.
                if (LISTA->tam == 2){
                    LISTA->inicio = tmp;
                    tmp->ant = NULL;
                }
                else{
                    tmp->ant->ant->prox = tmp;
                    tmp->ant = tmp->ant->ant;
                }

                LISTA->tam--;
                //free(aux);
            }
        }
    }

Browser other questions tagged

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