Repository navigation
Expand file tree
/
Copy pathtest.c
More file actions
270 lines (223 loc) · 8.56 KB
/
Copy pathtest.c
File metadata and controls
270 lines (223 loc) · 8.56 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <stdio.h>
#include <stdlib.h>
/* ─────────────────────────────────────────
SHARED NODE STRUCTURE
───────────────────────────────────────── */
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createNode(int val) {
Node* n = (Node*)malloc(sizeof(Node));
n->data = val;
n->next = NULL;
return n;
}
/* ─────────────────────────────────────────
LINKED LIST
───────────────────────────────────────── */
void insertFront(Node** head, int val) {
Node* n = createNode(val);
n->next = *head;
*head = n;
}
void insertBack(Node** head, int val) {
Node* n = createNode(val);
if (!*head) { *head = n; return; }
Node* cur = *head;
while (cur->next) cur = cur->next;
cur->next = n;
}
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; i++) cur = cur->next;
if (!cur) { printf(" [!] Position out of range\n"); return; }
Node* n = createNode(val);
n->next = cur->next;
cur->next = n;
}
void deleteVal(Node** head, int val) {
if (!*head) return;
if ((*head)->data == val) {
Node* tmp = *head;
*head = (*head)->next;
free(tmp); 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);
}
}
int maxVal(Node* head) {
int m = head->data;
while (head) { if (head->data > m) m = head->data; head = head->next; }
return m;
}
int minVal(Node* head) {
int m = head->data;
while (head) { if (head->data < m) m = head->data; head = head->next; }
return m;
}
int countOcc(Node* head, int val) {
int c = 0;
while (head) { if (head->data == val) c++; head = head->next; }
return c;
}
void displayLL(Node* head) {
printf(" List: ");
if (!head) { printf("(empty)\n"); return; }
while (head) { printf("%d", head->data); if (head->next) printf(" -> "); head = head->next; }
printf("\n");
}
void freeList(Node** head) {
Node* cur = *head, *tmp;
while (cur) { tmp = cur->next; free(cur); cur = tmp; }
*head = NULL;
}
/* ─────────────────────────────────────────
STACK (LIFO — only touch the head)
───────────────────────────────────────── */
void push(Node** top, int val) {
Node* n = createNode(val);
n->next = *top;
*top = n;
}
int pop(Node** top) {
if (!*top) { printf(" [!] Stack underflow\n"); return -1; }
Node* tmp = *top;
int val = tmp->data;
*top = (*top)->next;
free(tmp);
return val;
}
int peek(Node* top) {
if (!top) { printf(" [!] Stack empty\n"); return -1; }
return top->data;
}
void displayStack(Node* top) {
printf(" Stack (top->bottom): ");
if (!top) { printf("(empty)\n"); return; }
while (top) { printf("%d", top->data); if (top->next) printf(" -> "); top = top->next; }
printf("\n");
}
/* ─────────────────────────────────────────
QUEUE (FIFO — enqueue rear, dequeue front)
───────────────────────────────────────── */
void enqueue(Node** front, Node** rear, int val) {
Node* n = createNode(val);
if (!*rear) { *front = *rear = n; return; }
(*rear)->next = n;
*rear = n;
}
int dequeue(Node** front, Node** rear) {
if (!*front) { printf(" [!] Queue empty\n"); return -1; }
Node* tmp = *front;
int val = tmp->data;
*front = (*front)->next;
if (!*front) *rear = NULL; /* queue became empty */
free(tmp);
return val;
}
void displayQueue(Node* front) {
printf(" Queue (front->rear): ");
if (!front) { printf("(empty)\n"); return; }
while (front) { printf("%d", front->data); if (front->next) printf(" -> "); front = front->next; }
printf("\n");
}
/* ─────────────────────────────────────────
MAIN — TEST EVERYTHING
───────────────────────────────────────── */
int main() {
/* ══════════════════════════════════════
1. LINKED LIST TESTS
══════════════════════════════════════ */
printf("╔══════════════════════════════╗\n");
printf("║ LINKED LIST ║\n");
printf("╚══════════════════════════════╝\n");
Node* head = NULL;
printf("\n[+] insertBack: 10, 20, 30, 40, 50\n");
insertBack(&head, 10);
insertBack(&head, 20);
insertBack(&head, 30);
insertBack(&head, 40);
insertBack(&head, 50);
displayLL(head);
printf("\n[+] insertFront: 5\n");
insertFront(&head, 5);
displayLL(head);
printf("\n[+] insertAt position 3: value 99\n");
insertAt(&head, 99, 3);
displayLL(head);
printf("\n[+] deleteVal: 99\n");
deleteVal(&head, 99);
displayLL(head);
printf("\n[+] deleteVal: 5 (head)\n");
deleteVal(&head, 5);
displayLL(head);
printf("\n[+] insertBack: 20, 20 (duplicates)\n");
insertBack(&head, 20);
insertBack(&head, 20);
displayLL(head);
printf("\n[+] countOcc(20) = %d\n", countOcc(head, 20));
printf("[+] max = %d\n", maxVal(head));
printf("[+] min = %d\n", minVal(head));
freeList(&head);
printf("\n[+] After freeList:\n");
displayLL(head);
/* ══════════════════════════════════════
2. STACK TESTS
══════════════════════════════════════ */
printf("\n╔══════════════════════════════╗\n");
printf("║ STACK ║\n");
printf("╚══════════════════════════════╝\n");
Node* top = NULL;
printf("\n[+] push: 10, 20, 30, 40\n");
push(&top, 10);
push(&top, 20);
push(&top, 30);
push(&top, 40);
displayStack(top);
printf("\n[+] peek = %d\n", peek(top));
printf("\n[+] pop = %d\n", pop(&top));
displayStack(top);
printf("\n[+] pop = %d\n", pop(&top));
displayStack(top);
printf("\n[+] push: 99\n");
push(&top, 99);
displayStack(top);
printf("\n[+] pop all:\n");
while (top) printf(" popped: %d\n", pop(&top));
printf("\n[+] pop on empty stack:\n");
pop(&top);
/* ══════════════════════════════════════
3. QUEUE TESTS
══════════════════════════════════════ */
printf("\n╔══════════════════════════════╗\n");
printf("║ QUEUE ║\n");
printf("╚══════════════════════════════╝\n");
Node* front = NULL;
Node* rear = NULL;
printf("\n[+] enqueue: 10, 20, 30, 40\n");
enqueue(&front, &rear, 10);
enqueue(&front, &rear, 20);
enqueue(&front, &rear, 30);
enqueue(&front, &rear, 40);
displayQueue(front);
printf("\n[+] dequeue = %d\n", dequeue(&front, &rear));
displayQueue(front);
printf("\n[+] dequeue = %d\n", dequeue(&front, &rear));
displayQueue(front);
printf("\n[+] enqueue: 99\n");
enqueue(&front, &rear, 99);
displayQueue(front);
printf("\n[+] dequeue all:\n");
while (front) printf(" dequeued: %d\n", dequeue(&front, &rear));
printf("\n[+] dequeue on empty queue:\n");
dequeue(&front, &rear);
printf("\n--- All tests done ---\n");
return 0;
}