I am implementing a chained list of type "with head". It follows struct referring to the list and creating it in main ()
struct lista{
int info;
struct lista *prox;
};
typedef struct lista Lista;
int main ()
{
Lista *l, *aux;
l = (Lista*) malloc(sizeof(Lista));
l->prox = NULL;
//Trechos de código
retira_n(l, n);
}
Next follows the function that removes all occurrences of a given number "n" from the list and returns the list
Lista* retira_n(Lista *l, int n)
{
Lista *atual, *ant, *atual2;
int flag = 0;
atual = l->prox;
ant = l->prox;
while(atual != NULL)
{
if(atual == l->prox && atual->info == n)
{
l->prox = atual->prox;
free(atual);
atual = l->prox;
flag++;
continue;
}
else if(atual->info == n)
{
ant->prox = atual->prox;
flag++;
}
ant = atual;
atual = atual->prox;
}
if(flag == 0)
{
printf("O elemento nao esta na lista");
}
return l->prox;
}
The function is working in a partial way, that is, the list elements are not shown when I'm going to "start" the list, however, because I have not been able to deal with the free () function because if I am deallocating using the "current" pointer, for example, I will not be able to use it in other iterations to check the other elements of the list. I wanted a way to deallocate the element I want to delete and not just make the previous one point to the next element in the list.