1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>
// Simple linked list node
struct Node {
int data;
struct Node* next;
};
// Create a new node
struct Node* create_node(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
if (node != NULL) {
node->data = data;
node->next = NULL;
}
return node;
}
// Insert at head
void insert_head(struct Node** head, int data) {
struct Node* new_node = create_node(data);
new_node->next = *head;
*head = new_node;
}
// Find element
int find(struct Node* head, int target) {
while (head != NULL) {
if (head->data == target) return 1;
head = head->next;
}
return 0;
}
// Free list
void free_list(struct Node* head) {
while (head != NULL) {
struct Node* temp = head;
head = head->next;
free(temp);
}
}