struct node{
int elem ;
node* next;
};
typedef node* list;
void RemoveAllX(int x, list&l){
list copy = l;
list q;
while (l != NULL){
if (l -> elem == x){
q = l;
l = l -> next;
delete q;
}
else
l = l -> next;
}
l = copy;
}
What I'm trying to do is remove from the list every node that has an integer x as an element.
I would like that if I have a list let's say [4]->[1]->[1]->[3] and made the call RemoveAll(1, mylist) I get the list [4]->[3], however what I'm getting with this code is [4]->[trash]->[trash]->[3].
I'm assuming the line l = l -> next;
inside the if statement isn't working as I understood it should, any help appreciated