2
The following code aims to remove all occurrences of an integer in a list, (linked lists), returning at the end, the number of elements removed.
Give me the error of Segmentation fault. I appreciate any explanation.
typedef struct lligada {
int valor;
struct lligada *prox;
} *LInt;
int removeAll (LInt *l, int x){
int r = 0;
LInt pt = *l, ant = NULL;
while (pt != NULL){
if (pt -> valor == x){
ant -> prox = pt -> prox;
pt = pt -> prox;
r++;
}
else {
ant = pt;
pt = pt -> prox;
}
}
return r;
}
The chained list of struct lligada items, when removing an instance of lligada struct, must pass the address of that instance to free () otherwise, the result is a memory leak
– user3629249
Lint pt = *; >>> Llint pt = *l;
– user3629249