Problem with algorithm for removing duplicate numbers in double-chained list

1

Could someone help me with the following code?

void removeRepetidos(Lista *l)
{

    Elemento *aux = (*l);
    Elemento *aux2;
    Elemento *aux3;

    while(aux!=NULL)
    {
        aux2 = aux->prox;

        while(aux2 != NULL)
        {
            if(aux->num == aux2->num)
            {

                aux2->ant->prox = aux2->prox;

                if(aux2->prox!=NULL)
                {
                    aux2->prox->ant = aux2->ant;
                }


                aux3 = aux2;

            }


            aux2 = aux2->prox;
            free(aux3);
        }

        aux = aux->prox;
    }
}

It is crashing when running, removing the deallocation function free() the program works perfectly. I did not find any error in the logic of my code, I did several table tests.

    
asked by anonymous 29.10.2018 / 00:37

1 answer

2

The aux3 pointer is being deleted twice,

void removeRepetidos(Lista *l)
{
  Elemento *aux = (*l);
  Elemento *aux2;
  Elemento *aux3;

  while (aux!=NULL)
  {
    aux2 = aux->prox;
    while (aux2 != NULL)
    {
      if (aux->num == aux2->num)
      {
        aux2->ant->prox = aux2->prox;
        if (aux2->prox != NULL)
        {
          aux2->prox->ant = aux2->ant;
        }

        aux3 = aux2;
        aux2 = aux2->prox; // <---
        free(aux3);        // <--- incluir aqui
        continue;          // <---
      }

      aux2 = aux2->prox;
      // free(aux3); // <--- tirar daqui
    }

    aux = aux->prox;
  }

}

    
29.10.2018 / 02:58