Stack discipline
Introduction / Definition
Stack discipline follows last-in-first-out order. The most recently pushed item is popped first. Function calls, expression evaluation, undo operations, and depth-first search use stack behavior. Push, pop, and peek are standard operations.
Syntax / Structure / Formula
push(x); pop(); peek();
Detailed Explanation
A stack has a top position where insertion and removal occur. Underflow occurs when popping an empty stack.
Example
Example: Push A, B, C, then pop.
Output / Result
C is removed first, leaving A and B.
Important Points / Notes
- LIFO.
- Push/pop are typically O(1).
- Handle underflow.
Mini Practice Question
What is popped after A, B, C are pushed?
CheckpointTest Yourself