diff --git a/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js b/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js new file mode 100644 index 00000000..66bf136d --- /dev/null +++ b/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js @@ -0,0 +1,44 @@ +/** + * Evaluation of Postfix Expression + * Input:456*+ + * Output:34 + */ + +const Stack = require('../index'); + +function evaluatePostfixExpression(expression) { + let s = new Stack(); + for (let i = 0; i < expression.length; i++) { + const char = expression[i]; + if (!isNaN(char)) { + //if number push the char onto stack + s.push(Number(char)); + } else { + // if char is an operator then pop two elements from stack, evaluate them accordingly based on operator. + //push the result to stack + let val1 = s.pop(); + let val2 = s.pop() + switch (char) { + case '+': + s.push(val2 + val1); + break; + case '-': + s.push(val2 - val1); + break; + case '*': + s.push(val2 * val1); + break; + case '/': + s.push(val2 / val1); + break; + + } + } + } + //pop the value of postfix expression + return s.pop(); +} + +console.log(evaluatePostfixExpression("123+*8-")); // -3 + +console.log(evaluatePostfixExpression("12345*+*+")); // 47 \ No newline at end of file