๐ What Is a Stack?
A Stack is a Last-In, First-Out (LIFO) data structure.
This means the last element added is the first one to be removed โ just like a stack of plates ๐ฝ๏ธ.
๐ You can only access the top plate (element) at any given time.
In Java, the Stack is part of the java.util package:
import java.util.Stack;
โ๏ธ Example: Using Stack in Java
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack<String> browserHistory = new Stack<>();
browserHistory.push("Google");
browserHistory.push("YouTube");
browserHistory.push("GitHub");
System.out.println(browserHistory); // [Google, YouTube, GitHub]
System.out.println("Top: " + browserHistory.peek()); // GitHub
browserHistory.pop();
System.out.println("After back: " + browserHistory); // [Google, YouTube]
}
}
๐งฉ Operations:
push() โ Add element to top
pop() โ Remove top element
peek() โ View top element
empty() โ Check if stack is empty
search() โ Find position of an element
โ๏ธ Internal Working
Under the hood, Javaโs Stack class extends Vector, so itโs synchronized and thread-safe, but slower in single-threaded contexts.
A modern alternative is Deque, which provides faster stack operations:
import java.util.ArrayDeque;
Deque<Integer> stack = new ArrayDeque<>();
stack.push(10);
stack.push(20);
System.out.println(stack.pop()); // 20
๐ง Use ArrayDeque instead of Stack for better performance.
| Operation | Time Complexity |
|---|---|
| Push | O(1) |
| Pop | O(1) |
| Peek | O(1) |
| Search | O(n) |
๐ก Real-World Uses of Stack
Stacks are everywhere in computer science. Here are a few real-world scenarios:
๐งพ 1. Undo/Redo Mechanisms
Applications like text editors use stacks to track user actions.
- Undo Stack stores previous states.
- Redo Stack re-applies undone actions.
๐ 2. Browser Navigation
Every time you visit a page, itโs pushed onto the stack.
When you click โBack,โ it pops the last page and shows the previous one.
๐งฎ 3. Expression Evaluation
Stacks are used in:
Converting infix to postfix expressions
Evaluating arithmetic expressions
Example: (3 + 5) * 2 โ Processed using stacks.
๐ป 4. Function Call Stack
Each method call is pushed onto the call stack.
When the method finishes, itโs popped off. This is handled automatically by the JVM.
๐ 5. DFS Algorithms
Depth-First Search (DFS) in graphs uses a stack to keep track of visited nodes.
โ ๏ธ Common Mistakes
โ Forgetting that Stack is synchronized โ may impact performance.
โ Using Stack where ArrayDeque is better.
โ Popping from an empty stack โ throws EmptyStackException.
| Feature | Stack |
|---|---|
| Principle | LIFO |
| Core Operations | push(), pop(), peek() |
| Performance | O(1) for most |
| Implementation | Vector (synchronized) or ArrayDeque (recommended) |
| Best For | Backtracking, undo systems, expression parsing, recursion tracking |
๐ง Final Thought
A Stack is one of the simplest yet most powerful data structures.
It represents how programs manage memory, logic, and state.
Once you understand stacks, you understand recursion, backtracking, and control flow โ all critical to becoming an advanced Java developer. ๐ช
Top comments (0)