Learn › Class 12 › Computer Science / Informatics Practices › File handling and data structures (stack)
The stack data structure
Work it out on paper first. Then check the hint or the answer.
1. Evaluate the postfix expression 8 2 / 3 +.
Hint
Do the / first, then the +.
Answer and explanation
Answer: 7
8 / 2 = 4, then 4 + 3 = 7.
2. Starting empty: push 1, push 2, push 3, pop, pop, push 4. How many items are now in the stack?
Hint
Trace each step.
Answer and explanation
Answer: 2
[1, 2, 3] → [1] after two pops → [1, 4]: 2 items.
3. Evaluate the postfix expression 5 3 - 4 *.
Hint
Second-popped minus first-popped.
Answer and explanation
Answer: 8
5 − 3 = 2, then 2 × 4 = 8.
4. Write the postfix form of (A + B) * C.
Hint
The bracket is done first.
Answer and explanation
Answer: A B + C *
A + B becomes A B +, then multiply by C: A B + C *.