Mastery Points
0
Stacks and Queues Theory in dsa
Context & Logic
Stacks and Queues are linear data structures that follow specific orders for operations. Stacks follow LIFO (Last-In-First-Out), while Queues follow FIFO (First-In-First-Out).
Example
// Stack (LIFO)
const stack = [1, 2]; stack.push(3); stack.pop(); // 3 comes out
// Queue (FIFO)
const queue = [1, 2]; queue.push(3); queue.shift(); // 1 comes outStep-by-Step Logic
1
Stack: Use push() to add and pop() to remove (Top element).
2
Queue: Use push() to add (rear) and shift() to remove (front).
3
Use cases: recursion stack, undo/redo (Stack); BFS, task scheduling (Queue).
Complexity Metrics
Time Efficiency
O(1) for push, pop/shift
Memory Footprint
O(n)