3
I am implementing a chained list of type "with head". It follows struct referring to the list and its creation 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);
}
Then 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 elements of the list are not shown when I am going to "print" the list, however for not being able to find a way to displace the memory with the free() function, because if I go to displace using the "current" pointerfor example, I won’t be able to use it in other iterations to check the other elements of the list. I wanted a way to de-locate the element that I want to exclude and not just make the previous point to the next element on the list.
If I understand correctly you have a problem that is not so simple to solve a C. You have to control the lifetime of an element that can have multiple references. You would probably have to create a reference counter (and not allow cyclical references or have to use a weak reference). I don’t think you have any idea what I’m talking about. This shows that it is too complex a problem to attack in an exercise, unless the exercise is actually advanced and the intention is to apply the Counting. It has even more advanced techniques.
– Maniero