#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node
struct Node {
int data;
struct Node* next;
};
// Function to remove all nodes with a given key
void removeAllNodes(struct Node** head_ref, int key) {
struct Node** pcur = head_ref;
while (*pcur)
if ((*pcur)->data == key) {
struct Node* tmp = *pcur;
*pcur = (*pcur)->next;
free(tmp);
} else
pcur = &(*pcur)->next;
}
// Function to push a new node to the linked list
void push(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
// Function to print the linked list
void printList(struct Node* node) {
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
// Create the linked list 1->2->3->4->5->3->3
push(&head, 3);
push(&head, 3);
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
printf("Linked list before removal: ");
printList(head);
// Remove all nodes with data 3
removeAllNodes(&head, 3);
printf("Linked list after removal: ");
printList(head);
return 0;
}