Repository navigation
Expand file tree
/
Copy pathlist.c
More file actions
98 lines (87 loc) · 2.27 KB
/
Copy pathlist.c
File metadata and controls
98 lines (87 loc) · 2.27 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
// Always initialize head to NULL
Node* head = NULL;
Node* createNode(int val) {
Node* n = (Node*)malloc(sizeof(Node));
n->data = val;
n->next = NULL;
return n;
}
// Insert at front — O(1)
void insertFront(Node** head, int val) {
Node* n = createNode(val);
n->next = *head;
*head = n;
}
// Insert at back — O(n)
void insertBack(Node** head, int val) {
Node* n = createNode(val);
if (*head == NULL) { *head = n; return; }
Node* cur = *head;
while (cur->next != NULL) cur = cur->next;
cur->next = n;
}
// Insert at position k (0-indexed)
void insertAt(Node** head, int val, int k) {
if (k == 0) { insertFront(head, val); return; }
Node* cur = *head;
for (int i = 0; i < k-1 && cur != NULL; i++) cur = cur->next;
if (!cur) return; // position out of range
Node* n = createNode(val);
n->next = cur->next;
cur->next = n;
}
// Delete front
void deleteFront(Node** head) {
if (!*head) return;
Node* tmp = *head;
*head = (*head)->next;
free(tmp);
}
// Delete node with given value (first occurrence)
void deleteVal(Node** head, int val) {
if (!*head) return;
if ((*head)->data == val) { deleteFront(head); return; }
Node* cur = *head;
while (cur->next && cur->next->data != val)
cur = cur->next;
if (cur->next) {
Node* tmp = cur->next;
cur->next = tmp->next;
free(tmp);
}
}
// Delete ALL nodes with given value
void deleteAllVal(Node** head, int val) {
while (*head && (*head)->data == val) deleteFront(head);
Node* cur = *head;
while (cur && cur->next) {
if (cur->next->data == val) {
Node* tmp = cur->next;
cur->next = tmp->next;
free(tmp);
} else cur = cur->next;
}
}
// Search — returns 1 if found
int search(Node* head, int val) {
while (head) {
if (head->data == val) return 1;
head = head->next;
}
return 0;
}
void display(Node* head) {
while (head) { printf("%d ", head->data); head = head->next; }
printf("\n");
}
void freeList(Node** head) {
Node* cur = *head, *tmp;
while (cur) { tmp = cur->next; free(cur); cur = tmp; }
*head = NULL;
}