Doubt Double chained list with ordered insertion

Asked

Viewed 722 times

0

I created everything but my function of deleting is deleting the record but keeping in print the code 0.

Could someone help me find my mistake? I would also like to know if my algorithm was developed correctly, knowing that it was to be developed with double-chained list with ordered insertion.

1 answer

2


The problem is in the function exclusao.You are deleting the record but not decreasing the list size since the function print is using the variable lista->tamanho as a loop stop parameter for, output ends up being memory junk. Just put at the end of the function exclusao lista->tamanho-- that solves this problem.
By the way it looks like you’re using a cyclical list if you put NULL on the pointers can give segmentation failure.
With the corrections:

void exclusao(Lista* lista, int codigo)
{
    int achei = 0;
    Item* aux;
    Item* item = lista->primeiro;
    aux = item;
    while (aux && !achei)
    {
        if (aux->codigo == codigo)
        {
            if (aux == lista->primeiro)
            {
                if (aux == lista->ultimo)
                    lista->primeiro = lista->ultimo; //CASO 1
                else
                {
                    lista->primeiro = lista->primeiro->proximo; //CASO 2
                    lista->primeiro->anterior = lista->ultimo;
                }
            }
            else if (aux == lista->ultimo)
            { //CASO 3
                lista->ultimo = lista->ultimo->anterior;
                lista->ultimo->proximo = lista->primeiro;
            }
            else
            {//CASO 4
                aux->anterior->proximo = aux->proximo;
                aux->proximo->anterior = aux->anterior;
            }
            achei = 1;
            free(aux);
        }
        else aux = aux->proximo;
    }
    if (!achei)
        printf("\nCodigo inexistente!\n");
    else
        printf("\nExclusao realizada!\n");
   lista->tamanho--;
}

Browser other questions tagged

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