Stack Data Structure
LIFO (Last In, First Out) Principle
Your Name / Course
Introduction to Stack
• - A linear data structure
• - Works on LIFO principle
• - Last element inserted is the first removed
• - Analogy: Like a stack of plates
Stack Operations
• 1. Push(x): Insert element
• 2. Pop(): Remove top element
• 3. Peek/Top(): Show top element
• 4. isEmpty(): Checks if stack is empty
• 5. isFull(): Checks if stack is full
Stack Representation
• - Implemented using:
• • Array (fixed size)
• • Linked List (dynamic size)
• - TOP pointer keeps track of last element
Example: Push and Pop
• Push(10), Push(20), Push(30) → [10, 20, 30]
(Top=30)
• Pop() → removes 30 → [10, 20] (Top=20)
Applications of Stack
• - Undo/Redo in editors
• - Browser back/forward history
• - Expression evaluation
• - Function call stack
• - Balanced parentheses
Python Example
• stack = []
• stack.append(10)
• stack.append(20)
• stack.append(30)
• print('Stack:', stack)
• print('Popped:', stack.pop())
• print('After pop:', stack)
Conclusion
• - Stack is simple but powerful
• - Works on LIFO
• - Important in real-world applications