-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment3_dsa.cpp
More file actions
364 lines (305 loc) · 8.08 KB
/
Copy pathassignment3_dsa.cpp
File metadata and controls
364 lines (305 loc) · 8.08 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//ASSIGNMENT 3: STACKS
//Q1
#include <stdio.h>
#include <stdlib.h>
#define MAX 5 // Maximum size of the stack
int stack[MAX];
int top = -1;
// Function to check if stack is full
int isFull() {
return top == MAX - 1;
}
// Function to check if stack is empty
int isEmpty() {
return top == -1;
}
// Function to push an element into stack
void push() {
int value;
if (isFull()) {
printf("\nStack Overflow! Cannot push element.\n");
} else {
printf("Enter element to push: ");
scanf("%d", &value);
top++;
stack[top] = value;
printf("%d pushed into stack.\n", value);
}
}
// Function to pop an element from stack
void pop() {
if (isEmpty()) {
printf("\nStack Underflow! No element to pop.\n");
} else {
printf("Popped element: %d\n", stack[top]);
top--;
}
}
// Function to display the stack elements
void display() {
if (isEmpty()) {
printf("\nStack is empty.\n");
} else {
printf("\nStack elements are:\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
}
// Function to return the top element (peek)
void peek() {
if (isEmpty()) {
printf("\nStack is empty.\n");
} else {
printf("\nTop element is: %d\n", stack[top]);
}
}
// Main function
int main() {
int choice;
while (1) {
printf("\n--- Stack Menu ---\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Check if Empty\n");
printf("4. Check if Full\n");
printf("5. Display Stack\n");
printf("6. Peek (Top Element)\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: push(); break;
case 2: pop(); break;
case 3:
if (isEmpty()) printf("Stack is empty.\n");
else printf("Stack is not empty.\n");
break;
case 4:
if (isFull()) printf("Stack is full.\n");
else printf("Stack is not full.\n");
break;
case 5: display(); break;
case 6: peek(); break;
case 7: exit(0);
default: printf("Invalid choice! Try again.\n");
}
}
return 0;
}
//Q2
//Write a program to reverse a string using stack.
#include <stdio.h>
#include <string.h>
#define MAX 100
char stack[MAX]; //stack of characters (global variable)
int top = -1; //top of the stack (global variable)
// Push a character onto the stack
void push(char c) {
if (top == MAX - 1) {
printf("Stack Overflow!\n");
} else {
stack[++top] = c;
}
}
// Pop a character from the stack
void pop() {
if (top == -1) {
printf("Stack Underflow!\n");
} else {
putchar(stack[top--]);
}
}
//MAIN FUNCTION FOR Q2
int main() {
char str[MAX];
printf("Enter a string: ");
fgets(str, MAX, stdin); // (For simplicity; use fgets in practice)
// Push all characters of the string onto the stack
for (int i = 0; i < strlen(str); i++) {
push(str[i]);
}
}
//Q3
#include <stdio.h>
#include <string.h>
#define MAX 100
char stack[MAX]; //stack of characters (global variable)
int top = -1; //top of the stack (global variable)
// Push function
void push(char c) {
if (top == MAX - 1) {
printf("Stack Overflow!\n");
} else {
stack[++top] = c;
}
}
// Pop function
void pop() {
if (top == -1) {
printf("Stack Underflow!\n");
} else {
putchar(stack[top--]);
}
}
// Function to check if parentheses are balanced
int isBalanced(char exp[]) {
for (int i = 0; i < strlen(exp); i++) {
if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[') {
push(exp[i]);
}
else if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']') {
if (top == -1)
return 0; // Closing bracket before opening
pop();
if ((exp[i] == ')' && stack[top] != '(') ||
(exp[i] == '}' && stack[top] != '{') ||
(exp[i] == ']' && stack[top] != '[')) {
return 0; // Mismatched pair
}
}
}
// If stack is empty at the end, it's balanced
return (top == -1);
}
int main() {
char exp[MAX];
printf("Enter an expression: ");
fgets(exp, MAX, stdin ); // (for simplicity; use fgets in practice)
if (isBalanced(exp))
printf("Expression has balanced parentheses.\n");
else
printf("Expression does NOT have balanced parentheses.\n");
return 0;
}
//Q4
#include <stdio.h>
#include <ctype.h> // For isalnum()
#include <string.h>
#define MAX 100
char stack[MAX]; //stack of characters (global variable)
int top = -1; //top of the stack (global variable)
// Push function
void push(char c) {
if (top == MAX - 1)
printf("Stack Overflow!\n");
else
stack[++top] = c;
}
// Pop function
void pop() {
if (top == -1)
printf("Stack Underflow!\n");
else
putchar(stack[top--]);
}
// Function to return precedence of operators
int precedence(char c) {
if (c == '^')
return 3;
else if (c == '*' || c == '/')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return 0;
}
// Function to convert Infix to Postfix
void infixToPostfix(char infix[], char postfix[]) {
int i = 0, j = 0;
while ((c = infix[i++]) != '\0') {
if (isalnum(c)) { // Operand -> add to output
postfix[j++] = c;
}
else if (c == '(') {
push(c);
}
else if (c == ')') {
while (top != -1 && stack[top] != '(')
putchar(pop());
pop(); // remove '('
}
else { // Operator
while (top != -1 && precedence(stack[top]) >= precedence(c))
putchar(pop());
push(c);
}
}
// Pop remaining operators
while (top != -1)
putchar(pop());
postfix[j] = '\0';
}
int main() {
char infix[MAX], postfix[MAX];
printf("Enter an infix expression: ");
fgets(infix, MAX, stdin); // For simplicity; use fgets in practice
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}
//Q5
#include <stdio.h>
#include <ctype.h> // For isdigit()
#define MAX 100
char stack[MAX];
int top = -1;
// Push function
void push(int value) {
if (top == MAX - 1)
printf("Stack Overflow!\n");
else
stack[++top] = value;
}
// Pop function
void pop() {
if (top == -1) {
printf("Stack Underflow!\n");
} else {
putchar(stack[top--]);
}
}
// Function to evaluate postfix expression
int evaluatePostfix(char exp[]) {
int i = 0;
int val1, val2, result;
while ((c = exp[i++]) != '\0') {
if (isdigit(c)) {
// Convert character to integer and push
push(c - '0');
}
else if (c == ' ' || c == '\t') {
continue; // Ignore spaces
}
else {
// Operator encountered, pop two operands
val2 = pop();
val1 = pop();
switch (c) {
case '+': result = val1 + val2; break;
case '-': result = val1 - val2; break;
case '*': result = val1 * val2; break;
case '/': result = val1 / val2; break;
case '^': {
result = 1;
for (int j = 0; j < val2; j++)
result *= val1;
break;
}
default:
printf("Invalid operator: %c\n", c);
return -1;
}
push(result);
}
}
return pop(); // Final result
}
int main() {
char exp[MAX];
printf("Enter a postfix expression (e.g., 23*54*+9-): ");
fgets(exp, MAX, stdin); // For simplicity; use fgets in practice
int result = evaluatePostfix(exp);
printf("Result of Postfix Evaluation: %d\n", result);
return 0;
}