Stack in Data Structures

A Stack is one of the easiest data structures to learn. It works like a stack of plates: you add things on the top, and you remove things from the top. Think about how you stack plates in your kitchen.

  • You put one plate on top of another.
  • When you need a plate, you take the top one.
  • You never pull a plate from the bottom, right?

A Stack in programming works in the same way. We always work with the top item. That’s why we say, LIFO → Last In, First Out. The last thing you put in is the first thing you take out.

Why do we use a Stack?

Stacks show up everywhere, even if you don’t realise it:

  • The Undo button in apps
  • Browser Back button
  • Function calls in Java (Call Stack)
  • Checking balanced brackets like {}, (), []
  • Reversing strings
  • Expression evaluation

In all these cases, we only care about the most recent thing added – which makes Stack the perfect choice.

Basic Operations of a Stack

A stack usually supports these operations:

  • push(x) – add a value on top
  • pop() – remove and return the top value
  • peek() – check the top value without removing it
  • isEmpty() – check if the stack has no elements
  • isFull() – only for fixed array-based stacks

Java Code – Stack Implementation Using Arrays

Here, will build a stack in Java using a basic array. You’ll see how push, pop, and peek actually work internally, and you’ll understand why so many programming problems rely on stacks.

// Simple Stack implementation in Java using Array
public class StackUsingArray {

    private int[] stack;
    private int top;
    private int capacity;

    // Constructor
    public StackUsingArray(int size) {
        stack = new int[size];
        capacity = size;
        top = -1; // stack is empty
    }

    // Add element to the top
    public void push(int value) {
        if (isFull()) {
            System.out.println("Stack is full. Cannot push " + value);
            return;
        }
        top++;
        stack[top] = value;
    }

    // Remove and return the top element
    public int pop() {
        if (isEmpty()) {
            System.out.println("Stack is empty. Cannot pop.");
            return -1; // error code
        }
        int value = stack[top];
        top--;
        return value;
    }

    // Check top element without removing
    public int peek() {
        if (isEmpty()) {
            System.out.println("Stack is empty.");
            return -1;
        }
        return stack[top];
    }

    // Check if empty
    public boolean isEmpty() {
        return top == -1;
    }

    // Check if full
    public boolean isFull() {
        return top == capacity - 1;
    }

    // Print stack elements
    public void printStack() {
        if (isEmpty()) {
            System.out.println("Stack is empty");
            return;
        }

        System.out.print("Stack: ");
        for (int i = 0; i <= top; i++) {
            System.out.print(stack[i] + " ");
        }
        System.out.println();
    }

    // Main for testing
    public static void main(String[] args) {

        StackUsingArray stack = new StackUsingArray(5);

        stack.push(10);
        stack.push(20);
        stack.push(30);

        stack.printStack(); // Stack: 10 20 30

        System.out.println("Peek: " + stack.peek()); // 30

        System.out.println("Popped: " + stack.pop()); // 30

        stack.printStack(); // Stack: 10 20
    }
}

A stack is probably one of the simplest data structures, but don’t underestimate how often it’s used in real applications. It follows a very natural idea – the last thing you put in is the first thing that comes out.