DEV Community

Mohammed mhanna
Mohammed mhanna

Posted on • Edited on

๐Ÿง  Mastering Data Structures in Java โ€” Part 4: Stack

๐Ÿ” 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;

Enter fullscreen mode Exit fullscreen mode

โš™๏ธ 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]
    }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฉ 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
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  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)