-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack
More file actions
228 lines (175 loc) · 6.47 KB
/
Copy pathstack
File metadata and controls
228 lines (175 loc) · 6.47 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
int isEmpty(Node* top) { return top == NULL; }
void push(Node** top, int val){ Node* n = createNode(val); n->next = *top; *top = n; }
int pop (Node** top) {
if (isEmpty(*top)) return -1;
Node* tmp = *top; int val = tmp->data;
*top = (*top)->next; free(tmp); return val;
}
int peek(Node* top) { return isEmpty(top) ? -1 : top->data; }
void display(Node* top, const char* name) {
printf(" %s (top->bot): ", name);
if (!top) { printf("(empty)\n"); return; }
while (top) { printf("[%d]%s", top->data, top->next ? "->" : "\n"); top = top->next; }
}
Node* mergeTwoStacks(Node** s1, Node** s2) {
Node* s3 = NULL;
Node* tmp = NULL;
while (!isEmpty(*s1)) push(&tmp, pop(s1));
while (!isEmpty(tmp)) push(&s3, pop(&tmp));
while (!isEmpty(*s2)) push(&tmp, pop(s2));
/* Step 4: push tmp into S3 → S2 sits below S1 */
while (!isEmpty(tmp)) push(&s3, pop(&tmp));
return s3;
}
Node* copyStack(Node* src) {
Node* tmp = NULL;
Node* copy = NULL;
/* dump into tmp (reverses order) */
Node* cur = src;
while (cur) { push(&tmp, cur->data); cur = cur->next; }
/* dump tmp into copy (restores order) */
while (!isEmpty(tmp)) push(©, pop(&tmp));
return copy;
}
void sortStack(Node** top) {
Node* sorted = NULL;
while (!isEmpty(*top)) {
int cur = pop(top);
while (!isEmpty(sorted) && peek(sorted) < cur)
push(top, pop(&sorted));
push(&sorted, cur);
}
*top = sorted;
}
void insertAtBottom(Node** top, int val) {
if (isEmpty(*top)) { push(top, val); return; }
int tmp = pop(top);
insertAtBottom(top, val);
push(top, tmp);
}
void reverseStack(Node** top) {
if (isEmpty(*top)) return;
int val = pop(top);
reverseStack(top);
insertAtBottom(top, val);
}
int isPalindrome(char* s) {
int len = strlen(s);
Node* stk = NULL;
for (int i = 0; i < len / 2; i++) push(&stk, s[i]);
int start = (len % 2 == 0) ? len / 2 : len / 2 + 1;
for (int i = start; i < len; i++) {
if (pop(&stk) != s[i]) return 0;
}
return 1;
}
int matches(int open, int close) {
return (open == '(' && close == ')') ||
(open == '[' && close == ']') ||
(open == '{' && close == '}');
}
int isBalanced(char* s) {
Node* stk = NULL;
for (int i = 0; s[i]; i++) {
char c = s[i];
if (c=='(' || c=='[' || c=='{') push(&stk, c);
else if (c==')' || c==']' || c=='}') {
if (isEmpty(stk) || !matches(pop(&stk), c)) return 0;
}
}
return isEmpty(stk);
}
void deleteMiddle(Node** top, int k, int size) {
if (isEmpty(*top) || k == size / 2) { pop(top); return; }
int val = pop(top);
deleteMiddle(top, k + 1, size);
push(top, val);
}
int stackSize(Node* top) {
int c = 0; while (top) { c++; top = top->next; } return c;
}
Node* sumTwoStacks(Node* s1, Node* s2) {
Node* s3 = NULL;
Node* tmp1 = NULL, *tmp2 = NULL;
Node* cur = s1; while (cur) { push(&tmp1, cur->data); cur = cur->next; }
cur = s2; while (cur) { push(&tmp2, cur->data); cur = cur->next; }
while (!isEmpty(tmp1) || !isEmpty(tmp2)) {
int a = isEmpty(tmp1) ? 0 : pop(&tmp1);
int b = isEmpty(tmp2) ? 0 : pop(&tmp2);
push(&s3, a + b);
}
return s3;
}
int main() {
printf("\n╔══════════════════════════════════════╗\n");
printf("║ STACK EXAM IDEAS — ALL DEMOS ║\n");
printf("╚══════════════════════════════════════╝\n");
printf("\n── IDEA 1: Merge two stacks into a third ──\n");
Node* s1 = NULL; push(&s1,10); push(&s1,20); push(&s1,30);
Node* s2 = NULL; push(&s2,40); push(&s2,50); push(&s2,60);
display(s1, "S1 before");
display(s2, "S2 before");
Node* s3 = mergeTwoStacks(&s1, &s2);
display(s3, "S3 merged ");
/* free s3 */
while (!isEmpty(s3)) pop(&s3);
printf("\n── IDEA 2: Copy a stack ──\n");
Node* orig = NULL; push(&orig,1); push(&orig,2); push(&orig,3); push(&orig,4);
Node* clone = copyStack(orig);
display(orig, "Original ");
display(clone, "Copy ");
while (!isEmpty(orig)) pop(&orig);
while (!isEmpty(clone)) pop(&clone);
printf("\n── IDEA 3: Sort a stack (top = smallest) ──\n");
Node* st = NULL; push(&st,34); push(&st,3); push(&st,31); push(&st,98); push(&st,92); push(&st,23);
display(st, "Before sort");
sortStack(&st);
display(st, "After sort ");
while (!isEmpty(st)) pop(&st);
printf("\n── IDEA 4: Reverse a stack in place ──\n");
Node* rv = NULL; push(&rv,1); push(&rv,2); push(&rv,3); push(&rv,4); push(&rv,5);
display(rv, "Before reverse");
reverseStack(&rv);
display(rv, "After reverse");
while (!isEmpty(rv)) pop(&rv);
printf("\n── IDEA 5: Palindrome check ──\n");
char* words[] = { "racecar", "hello", "level", "world", "madam" };
for (int i = 0; i < 5; i++)
printf(" \"%s\" -> %s\n", words[i], isPalindrome(words[i]) ? "PALINDROME" : "NOT palindrome");
printf("\n── IDEA 6: Balanced parentheses ──\n");
char* exprs[] = { "({[]})", "([)]", "{[]}", "(((" , "(())" };
for (int i = 0; i < 5; i++)
printf(" \"%s\" -> %s\n", exprs[i], isBalanced(exprs[i]) ? "BALANCED" : "NOT balanced");
printf("\n── IDEA 7: Delete middle element ──\n");
Node* mid = NULL;
push(&mid,1); push(&mid,2); push(&mid,3); push(&mid,4); push(&mid,5);
display(mid, "Before delete middle");
int sz = stackSize(mid);
deleteMiddle(&mid, 0, sz);
display(mid, "After delete middle");
while (!isEmpty(mid)) pop(&mid);
printf("\n── IDEA 8: Element-wise sum of two stacks ──\n");
Node* a = NULL; push(&a,1); push(&a,2); push(&a,3);
Node* b = NULL; push(&b,10); push(&b,20); push(&b,30);
display(a, "Stack A ");
display(b, "Stack B ");
Node* sum = sumTwoStacks(a, b);
display(sum, "A + B ");
while (!isEmpty(a)) pop(&a);
while (!isEmpty(b)) pop(&b);
while (!isEmpty(sum)) pop(&sum);
printf("\n--- Done ---\n");
return 0;
}