#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createNode(int data) {
Node newNode = (Node)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertNode(Node head, int data) {
Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}
void displayList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
Node filterList(Node head, int target) {
Node* current = head;
Node* previous = NULL;
while (current != NULL && current->data != target) {
previous = current;
current = current->next;
}
if (current == NULL) {
return head;
}
if (previous == NULL) {
head = current->next;
} else {
previous->next = current->next;
}
free(current);
return head;
}
int main() {
Node* head = NULL;
insertNode(&head, 10);
insertNode(&head, 20);
insertNode(&head, 30);
insertNode(&head, 40);
printf("Original List: ");
displayList(head);
int target = 30;
head = filterList(head, target);
printf("List after filtering %d: ", target);
displayList(head);
return 0;
}