Name:=Kaveri Nagare
UID:=UIT2025005
_____________________________________________________________________________________
#include<stdio.h> }
#include<stdlib.h> return top->data;
#include<ctype.h> }
#include<string.h>
void Display() {
#define MAX 50 if (IsEmpty()) {
printf("Stack is empty\n");
struct Node { return;
int data; }
struct Node* next; struct Node* temp = top;
}; printf("Stack elements are: ");
struct Node* top = NULL; while (temp != NULL) {
int count = 0; printf("%d ", temp->data);
temp = temp->next;
int IsEmpty() { }
return top == NULL; printf("\n");
} }
void push(int value) { int size() {
struct Node* newnode = (struct return count;
Node*)malloc(sizeof(struct Node)); }
newnode->data = value;
newnode->next = top; char stack[MAX];
top = newnode; int top2 = -1;
count++;
printf("%d pushed onto the stack\n", value); void push2(char x) {
} stack[++top2] = x;
}
int pop() {
if (IsEmpty()) { char pop2() {
printf("Stack underflow\n"); if (top2 == -1) return -1;
return -1; return stack[top2--];
} }
int popped = top->data;
struct Node* temp = top; int priority(char x) {
top = top->next; if (x == '(') return 0;
free(temp); if (x == '+' || x == '-') return 1;
count--; if (x == '*' || x == '/') return 2;
return popped; return -1;
} }
int DisplayTop() { void infixToPostfix(char infix[], char postfix[]) {
if (IsEmpty()) { char *e = infix;
printf("Stack is empty\n"); char x;
return -1; int k = 0;
case '/': valStack[++valTop] = op1 / op2;
while (*e != '\0') { break;
if (isalnum(*e)) { }
postfix[k++] = *e; }
} i++;
else if (*e == '(') { }
push2(*e); return valStack[valTop--];
} }
else if (*e == ')') {
while ((x = pop2()) != '(') { int main() {
postfix[k++] = x; int ch, val, result;
} char infix[MAX], postfix[MAX];
}
else { while (1) {
while (top2 != -1 && priority(stack[top2]) printf("\n--- Main Menu ---\n");
>= priority(*e)) { printf("1. Push into the stack\n");
postfix[k++] = pop2(); printf("2. Pop from the stack\n");
} printf("3. Display Top\n");
push2(*e); printf("4. Display current count\n");
} printf("5. Display the stack elements\n");
e++; printf("6. Convert Infix to Postfix\n");
} printf("7. Evaluate Postfix Expression\n");
while (top2 != -1) { printf("8. Exit\n");
postfix[k++] = pop2(); printf("Enter your Choice: ");
} scanf("%d", &ch);
postfix[k] = '\0';
} switch (ch) {
case 1:
int evalPostfix(char exp[]) { printf("Enter the value to push: ");
int i = 0; scanf("%d", &val);
char ch; push(val);
int valStack[MAX], valTop = -1; break;
int op1, op2;
case 2: {
while ((ch = exp[i]) != '\0') { int popped = pop();
if (isdigit(ch)) { if (popped != -1)
valStack[++valTop] = ch - '0'; printf("%d is popped from the stack\n",
} popped);
else { break;
op2 = valStack[valTop--]; }
op1 = valStack[valTop--];
switch (ch) { case 3: {
case '+': valStack[++valTop] = op1 + int topVal = DisplayTop();
op2; break; if (topVal != -1)
case '-': valStack[++valTop] = op1 - op2; printf("%d is the top element\n",
break; topVal);
case '*': valStack[++valTop] = op1 * break;
op2; break; }
case 4: --- Main Menu ---
printf("The current count of stack is: %d\ 1. Push into the stack
n", size()); 2. Pop from the stack
break; 3. Display Top
4. Display current count
case 5: 5. Display the stack elements
Display(); 6. Convert Infix to Postfix
break; 7. Evaluate Postfix Expression
8. Exit
case 6: Enter your Choice: 1
printf("Enter infix expression: "); Enter the value to push: 34
scanf("%s", infix); 34 pushed onto the stack
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix); --- Main Menu ---
break; 1. Push into the stack
2. Pop from the stack
case 7: 3. Display Top
printf("Enter postfix expression: "); 4. Display current count
scanf("%s", postfix); 5. Display the stack elements
result = evalPostfix(postfix); 6. Convert Infix to Postfix
printf("Result: %d\n", result); 7. Evaluate Postfix Expression
break; 8. Exit
Enter your Choice: 6
case 8: Enter infix expression: (A+B)*(C*D)
printf("Exiting...\n"); Postfix expression: AB+CD**
return 0;
--- Main Menu ---
default: 1. Push into the stack
printf("Invalid choice!\n"); 2. Pop from the stack
} 3. Display Top
} 4. Display current count
return 0; 5. Display the stack elements
} 6. Convert Infix to Postfix
OUTPUT:= 7. Evaluate Postfix Expression
8. Exit
--- Main Menu --- Enter your Choice: 7
1. Push into the stack Enter postfix expression: 34+5*
2. Pop from the stack Result: 35
3. Display Top
4. Display current count --- Main Menu ---
5. Display the stack elements 1. Push into the stack
6. Convert Infix to Postfix 2. Pop from the stack
7. Evaluate Postfix Expression 3. Display Top
8. Exit 4. Display current count
Enter your Choice: 1 5. Display the stack elements
Enter the value to push: 12 6. Convert Infix to Postfix
12 pushed onto the stack 7. Evaluate Postfix Expression
8. Exit
Enter your Choice: 8
Exiting...