We're sorry this tutorial wasn't helpful. Please tell us what we can do better.
Stack Advantages and Disadvantages
Last updated:
Stack Data Structure
A stack is a linear collection with one open end, the top. Elements are pushed onto the top and popped from the top, so they leave in LIFO order (Last In, First Out): the element added most recently is removed first.
That single restriction is the source of both the strengths and the weaknesses listed below.
Advantages of Stack Data Structure
Constant-time operations:push, pop, peek and isEmpty work only at the top, so each takes O(1) time no matter how many elements are stored (amortized O(1) for push on a growable array).
Simple to build and hard to misuse: an array plus a single index, or a linked list whose head is the top, is enough. With only a few operations available, there is little room for mistakes.
Natural fit for nested work: anything that must be finished in the reverse order it was started (method calls, nested brackets, HTML tags) maps directly onto a stack.
Supports recursion and backtracking: the call stack is what makes recursion possible, and an explicit stack lets you write depth-first search or backtracking without recursion.
Undo and history: pushing each action lets a program reverse the most recent one first, as in editor undo or a browser's back button.
Low overhead: an array-based stack stores nothing but its elements and one index.
Disadvantages of Stack Data Structure
The same restriction to one end limits what a stack can do:
Only the top is reachable: to read an element further down you must pop everything above it, which is O(n) and changes the stack.
No random access or efficient search: there is no access by index, and finding a value takes O(n).
Overflow in fixed-size stacks: an array-based stack with a fixed capacity cannot accept more elements once it is full; a resizable array or a linked list avoids this.
Limited call stack: the JVM's call stack has a fixed size per thread, so very deep recursion throws StackOverflowError.
Wrong tool for FIFO work: when items must be processed in arrival order, a queue is needed instead.
Frequently Asked Questions (FAQ)
Q1. What are the main advantages of a stack?
All main operations are O(1).
It is simple to implement with an array or a linked list.
It naturally models nested work such as function calls, brackets and undo.
Memory is managed automatically in order: the last item added is the first freed.
Q2. What are the main disadvantages of a stack?
Only the top element is accessible; reaching another element means popping the ones above it.
A fixed-size array stack can overflow.
Searching for a value takes O(n) and destroys the stack unless you copy it.
Q3. Why is restricted access considered both an advantage and a disadvantage?
Allowing changes only at the top keeps the structure simple, fast and hard to misuse, which suits LIFO problems. The same restriction makes the stack a poor fit for tasks that need random access or searching.
Q4. Array-based or linked-list-based stack: which is better?
An array-based stack is faster in practice and uses less memory, but a fixed array can fill up (a resizable array avoids this). A linked-list stack never fills up until memory runs out, but each element needs an extra node object. In Java, ArrayDeque (a resizable array) is usually the best choice.
Q5. Write a stack using a linked list in Java.
LinkedStack.java
public class LinkedStack {
private static class Node {
int data;
Node next;
Node(int data, Node next) { this.data = data; this.next = next; }
}
private Node top;
private int size;
public void push(int value) {
top = new Node(value, top);
size++;
}
public int pop() {
if (top == null) {
throw new IllegalStateException("Stack is empty");
}
int value = top.data;
top = top.next;
size--;
return value;
}
public int size() {
return size;
}
public static void main(String[] args) {
LinkedStack stack = new LinkedStack();
stack.push(1);
stack.push(2);
System.out.println(stack.pop() + ", size " + stack.size()); // 2, size 1
}
}
Q6. How do you search for a value in a stack without losing its contents?
Pop elements into a temporary stack while searching, then push them back:
SearchStack.java
import java.util.ArrayDeque;
import java.util.Deque;
static boolean contains(Deque<Integer> stack, int value) {
Deque<Integer> temp = new ArrayDeque<>();
boolean found = false;
while (!stack.isEmpty()) {
int top = stack.pop();
temp.push(top);
if (top == value) {
found = true;
break;
}
}
while (!temp.isEmpty()) {
stack.push(temp.pop()); // restore the original order
}
return found;
}
This takes O(n) time and O(n) extra space, which shows why stacks are not suited to searching.
Q7. Why is recursion related to the disadvantages of stacks?
Every recursive call uses space on the call stack, which has a fixed size per thread. Deep recursion can therefore throw a StackOverflowError. Converting the recursion to a loop with an explicit Deque stack avoids this limit.
Q8. Write Java code to reverse a string using a stack.
ReverseWithStack.java
import java.util.ArrayDeque;
import java.util.Deque;
static String reverse(String text) {
Deque<Character> stack = new ArrayDeque<>();
for (char c : text.toCharArray()) {
stack.push(c);
}
StringBuilder result = new StringBuilder();
while (!stack.isEmpty()) {
result.append(stack.pop());
}
return result.toString();
}
Because of LIFO order, characters come out in reverse. It takes O(n) time and space.
Q9. When should you not use a stack?
When you need first-in, first-out order (use a queue), random access by index (use an array or ArrayList), fast lookup by value (use a HashSet or HashMap), or sorted order (use a TreeSet or a priority queue).
Q10. Is java.util.Stack thread-safe, and should you use it?
Yes, its methods are synchronized, but that makes it slower, and it also inherits index-based methods from Vector. For single-threaded code use ArrayDeque; for concurrent code use ConcurrentLinkedDeque or LinkedBlockingDeque.