Passionate about coding, I specialize in C programming and have a strong foundation in Data Structures and Algorithms (DSA). My projects reflect my problem-solving abilities, from implementing fundamental data structures like linked lists and binary trees to designing algorithms that tackle complex computations efficiently. I have explored both theoretical and applied aspects of DSA, finding efficient ways to work with stacks and queues, especially for postfix evaluations. My focus is on writing clean, efficient code, whether I’m managing memory dynamically or applying sorting and searching algorithms to real-world problems, continuously building skills to innovate in the field.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
int stack[MAX];
int top = -1,i=0;
void push(int val) {
if (top == MAX - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = val;
}
}
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
} else {
return stack[top--];
}
}
int evaluatePostfix(char* exp) {
int length = strlen(exp); // Get the length of the expression
for (i = 0; i < length; i++) {
char ch = exp[i];
// If the character is a digit, push it to the stack
if (isdigit(ch)) {
push(ch - '0');
}
// If the character is an operator, pop two elements from the stack and perform the operation
else {
int val1 = pop();
int val2 = pop();
switch (ch) {
case '+': push(val2 + val1); break;
case '-': push(val2 - val1); break;
case '*': push(val2 * val1); break;
case '/': push(val2 / val1); break;
}
}
}
return pop(); // Return the final result
}
int main() {
int n;
scanf("%d",&n);
char input[n];
scanf("%s",input);
printf("Postfix Evaluation Result: %d\n", evaluatePostfix(input));
return 0;
}