Learn › Class 12 › Computer Science / Informatics Practices › File handling and data structures (stack)
The stack data structure
Example 1: Trace a sequence of operations
Open one step at a time. Try the next step yourself first.
- Step 1. Start with an empty stack [].
Show step 2
push 5, push 8: [5, 8].
Show step 3
pop removes 8: [5].
Show step 4
push 3, push 7: [5, 3, 7].
Show step 5
pop removes 7: the stack is [5, 3].
Example 2: Evaluate the postfix expression 6 2 3 + *
Open one step at a time. Try the next step yourself first.
- Step 1. Push 6, 2, 3: [6, 2, 3].
Show step 2
+ pops 3 and 2, pushes 5: [6, 5].
Show step 3
* pops 5 and 6, pushes 30: [30].
Show step 4
The answer is 30.