Stack
Stack
Stacks and Queues are fundamental linear data structures that store and organize data in
specific orders. They are used heavily in algorithms, memory management, and real-world
systems like job scheduling and browser history tracking.
🧠 Operations:
● push(item) → Add item to the top
🛠 Java Example:
java
CopyEdit
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.pop();
🔍 Use Cases:
● Function call stack
● Undo/Redo systems
🧠 Operations:
● enqueue(item) → Add item at the end
🛠 Java Example:
java
CopyEdit
Queue<Integer> queue = new LinkedList<>();
queue.add(10);
queue.remove();
🔍 Use Cases:
● CPU/task scheduling
● Print queues
● Buffers in networking
🔄 Variations
Type Description
Deque Double-ended queue (can add/remove both
ends)
⚖️ Time Complexity
Operation Stac Queu
k e
Note: Priority queues and deques may have different complexities depending on
implementation.
✅ Summary
● Stacks use LIFO: Last in is the first out.
● Java provides built-in support with Stack, Queue, Deque, and PriorityQueue.