Double chained list in C using remove function

Asked

Viewed 64 times

-2

Could someone help me because I’m not getting the function of remove, I did everything right, but I’m not succeeding, need not give me the code, just could guide me what is wrong and how to solve. I thank you in advance! I’ll put the code snippet:

typedef struct ELEMENTO{
    int valor;
    struct ELEMENTO * proximo;
    struct  ELEMENTO * anterior;
}ELEMENTO;

typedef struct LISTA{
    ELEMENTO * primeiro;
    ELEMENTO * ultimo;
    int quant_elementos;
}LISTA;

void remover(LISTA * lista, int valor){
    if(list->primeiro->valor == valor){
        ELEMENTO * aux = lista->primeiro;
        lista->primeiro = lista->primeiro->proximo;
        lista->primeiro->anterior = NULL;
        free(aux);
        lista->quant_elementos--;
    }else{
        ELEMENTO * aux = lista->primeiro->proximo;
        while(aux->proximo != NULL){
            if (aux->valor == valor)
            {
                aux->anterior->proximo = aux->proximo;
                aux->proximo->anterior = aux->anterior;
                free(aux);
                lista->quant_elementos--;
                break;
            }
            aux = aux->proximo;
        }
    }
}
  • It works if you are removing the last?

  • I don’t think so, buddy, I don’t know, I’m still studying!

1 answer

1

Substitute:

void remover(LISTA * lista, int valor){
    if(list->primeiro->valor == valor){
//...

For:

void remover(LISTA * lista, int valor){
    if(lista->primeiro->valor == valor){
//...

There are some other logic problems in its removal function. Cases still need to be supported when the list is empty, or when value is in the last node, for example. However, to compile this section simply replace list->primeiro->valor for lista->primeiro->valor.

Browser other questions tagged

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