#include <stdio.h>
#include <stdlib.h>
typedef struct lligada {
int valor;
struct lligada *prox;
} *LInt;
void addHead (LInt *l, int val){
LInt newHead
= malloc(sizeof(*newHead
)); newHead->valor = val;
newHead->prox = *l;
*l = newHead;
}
void print(LInt list){
LInt curr = list;
while (curr != NULL){
printf("%d -> ", curr
->valor
); curr = curr->prox;
}
}
int removeAll (LInt *l, int x){
int r = 0;
LInt pt = *l, ant = NULL;
while (pt != NULL){
if (pt -> valor == x){
if (ant == NULL){
*l = pt->prox;
}
else {
ant -> prox = pt -> prox;
}
LInt toDelete = pt;
pt = pt -> prox;
r++;
}
else {
ant = pt;
pt = pt -> prox;
}
}
return r;
}
int main() {
LInt list = NULL;
addHead(&list, 10);
addHead(&list, 20);
addHead(&list, 15);
addHead(&list, 10);
addHead(&list, 12);
addHead(&list, 10);
addHead(&list, 10);
addHead(&list, 13);
addHead(&list, 10);
print(list);
int removed = removeAll(&list, 10);
printf("\nRemoved nodes: %d\n", removed
); print(list);
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnR5cGVkZWYgc3RydWN0IGxsaWdhZGEgewogICAgaW50IHZhbG9yOwogICAgc3RydWN0IGxsaWdhZGEgKnByb3g7Cn0gKkxJbnQ7Cgp2b2lkIGFkZEhlYWQgKExJbnQgKmwsIGludCB2YWwpewogICAgTEludCBuZXdIZWFkID0gbWFsbG9jKHNpemVvZigqbmV3SGVhZCkpOwogICAgbmV3SGVhZC0+dmFsb3IgPSB2YWw7CiAgICBuZXdIZWFkLT5wcm94ID0gKmw7CiAgICAqbCA9IG5ld0hlYWQ7Cn0KCnZvaWQgcHJpbnQoTEludCBsaXN0KXsKICAgIExJbnQgY3VyciA9IGxpc3Q7CiAgICB3aGlsZSAoY3VyciAhPSBOVUxMKXsKICAgICAgICBwcmludGYoIiVkIC0+ICIsIGN1cnItPnZhbG9yKTsKICAgICAgICBjdXJyID0gY3Vyci0+cHJveDsKICAgIH0KICAgIHByaW50ZigiTlVMTCIpOwp9CgppbnQgcmVtb3ZlQWxsIChMSW50ICpsLCBpbnQgeCl7CiAgICBpbnQgciA9IDA7CiAgICBMSW50IHB0ID0gKmwsIGFudCA9IE5VTEw7CiAgICB3aGlsZSAocHQgIT0gTlVMTCl7CiAgICAgICAgaWYgKHB0IC0+IHZhbG9yID09IHgpewogICAgICAgICAgICBpZiAoYW50ID09IE5VTEwpewogICAgICAgICAgICAgICAgKmwgPSBwdC0+cHJveDsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIHsKICAgICAgICAgICAgICAgIGFudCAtPiBwcm94ID0gcHQgLT4gcHJveDsKICAgICAgICAgICAgfQogICAgICAgICAgICBMSW50IHRvRGVsZXRlID0gcHQ7CiAgICAgICAgICAgIHB0ID0gcHQgLT4gcHJveDsKICAgICAgICAgICAgZnJlZSh0b0RlbGV0ZSk7CiAgICAgICAgICAgIHIrKzsKICAgICAgICB9CiAgICAgICAgZWxzZSB7CiAgICAgICAgICAgIGFudCA9IHB0OwogICAgICAgICAgICBwdCA9IHB0IC0+IHByb3g7CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiByOwp9CgppbnQgbWFpbigpIHsKICAgIExJbnQgbGlzdCA9IE5VTEw7CiAgICBhZGRIZWFkKCZsaXN0LCAxMCk7CiAgICBhZGRIZWFkKCZsaXN0LCAyMCk7CiAgICBhZGRIZWFkKCZsaXN0LCAxNSk7CiAgICBhZGRIZWFkKCZsaXN0LCAxMCk7CiAgICBhZGRIZWFkKCZsaXN0LCAxMik7CiAgICBhZGRIZWFkKCZsaXN0LCAxMCk7CiAgICBhZGRIZWFkKCZsaXN0LCAxMCk7CiAgICBhZGRIZWFkKCZsaXN0LCAxMyk7CiAgICBhZGRIZWFkKCZsaXN0LCAxMCk7CgogICAgcHJpbnQobGlzdCk7CiAgICBpbnQgcmVtb3ZlZCA9IHJlbW92ZUFsbCgmbGlzdCwgMTApOwogICAgcHJpbnRmKCJcblJlbW92ZWQgbm9kZXM6ICVkXG4iLCByZW1vdmVkKTsKICAgIHByaW50KGxpc3QpOwp9Cg==