← Back to Data Structures & Algorithms
Question 438
Evaluate a Postfix Expression
Evaluate a Postfix Expression
In postfix notation the operator comes after its two operands, so3 4 +means 3 + 4. A stack evaluates it in one pass. The Deque, the split into tokens, the loop andisOperator()are already written. Requirements: 1. For a number token, push its int value onto the stack. 2. For an operator token, pop two values and push the result of +, -, * or / on them. The first pop is the right operand and the second pop is the left one, which matters for - and /. 3. Use integer division for /, and return the single value left on the stack, or -1 if there is none. Do NOT write a main method.
Expected Output:
7 14 5 7
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Data Structures & AlgorithmsStacks
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.