#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <math.h> struct stack{ int data; struct stack * link; }; struct stack * top; int evaluate(char exp[]); void push(int x); int pop(); int main(){ char exp[] = "3 2 * 5 4 * +"; printf("%d",evaluate(exp)); return 0; } int evaluate(char exp[]){ int i; int len = strlen(exp); for(i = 0; i<=len;i++){ if(exp[i]==' '){ continue; } else if(isdigit(exp[i])==1){ int num = 0; while(isdigit(exp[i])){ num = num*10 + (int)(exp[i]-'0'); i++; } i--; push(num); // Pushing the operand to the stack }else{ int val2 = pop(); int val1 = pop(); switch(exp[i]){ case '+': push(val1 + val2); break; case '-': push(val1 - val2); break; case '*': push(val1 * val2); break; case '/': push(val1/val2); break; } } } return pop(); } void push(int x){ struct stack * temp = (struct stack *)malloc(sizeof(struct stack)); (*temp).data = x; (*temp).link = top; top = temp; return; } int pop(){ struct stack * temp = top; int ans = (*temp).data; top = (*temp).link; return ans; }

Can you please tell me where I have gone wrong. Much thanks in advance !