Learn › Class 12 › Computer Science / Informatics Practices › File handling and data structures (stack)
The stack data structure
Remember
- A stack is LIFO; push and pop happen at the top.
- Python: append() to push, pop() to pop, [-1] to peek; check for underflow.
- Postfix: push operands; on an operator pop two, apply, push the result.
Quick self-check
How can a stack reverse a string?
Check
Push each character, then pop them all; they come out in reverse order.
What does stack[-1] do?
Check
It reads the top item without removing it.