Java Stack Implementation Using Arrays

This implementation demonstrates how to create a Stack data structure using an array in Java. A stack follows the Last-In-First-Out (LIFO) principle.

The Stack Class

class Stack {
  private int top; // Represents the index position of the topmost element in the stack
  private int maxSize; // Represents the maximum number of elements that can be stored in the stack
  private int[] arr;

  Stack(int maxSize) {
    this.top = -1; // Top is -1 when the stack is created
    this.maxSize = maxSize;
    arr = new int[maxSize];
  }

  // Checks if the stack is full
  public boolean isFull() {
    if (top >= (maxSize - 1)) {
      return true;
    }
    return false;
  }

  // Adds a new element to the top of the stack
  public boolean push(int data) {
    if (isFull()) {
      return false;
    } else {
      arr[++top] = data;
      return true;
    }
  }

  // Returns the topmost element of the stack
  public int peek() {
    if (isEmpty())
      return Integer.MIN_VALUE;
    else
      return arr[top];
  }

  // Displays all the elements of the stack
  public void display() {
    if (isEmpty())
      System.out.println("Stack is empty!");
    else {
      System.out.println("Displaying stack elements");
      for (int index = top; index >= 0; index--) {
        System.out.println(arr[index]); // Accessing element at position index
      }
    }
  }

  // Checks if the stack is empty
  public boolean isEmpty() {
    if (top < 0) {
      return true;
    }
    return false;
  }

  // Removes the element from the top of the stack
  public int pop() {
    if (isEmpty())
      return Integer.MIN_VALUE;
    else
      return arr[top--];
  }
}

The Tester Class

class Tester {
  public static void main(String args[]) {
    Stack stack = new Stack(5);
    System.out.println("Stack created.\n");

    if (stack.push(1))
      System.out.println("The element is pushed to the stack!\n");
    else
      System.out.println("Stack is full!\n");

    if (stack.push(2))
      System.out.println("The element is pushed to the stack!\n");
    else
      System.out.println("Stack is full!\n");

    if (stack.push(3))
      System.out.println("The element is pushed to the stack!\n");
    else
      System.out.println("Stack is full!\n");

    if (stack.push(4))
      System.out.println("The element is pushed to the stack!\n");
    else
      System.out.println("Stack is full!\n");

    if (stack.push(5))
      System.out.println("The element is pushed to the stack!\n");
    else
      System.out.println("Stack is full!\n");

    stack.display();

    if (stack.push(6))
      System.out.println("The element is pushed to the stack!\n");
    else
      System.out.println("Stack is full!\n");

    System.out.println("The top element is : " + stack.peek());

    int poppedElement = stack.pop();
    if (poppedElement == Integer.MIN_VALUE)
      System.out.println("Stack is empty\n");
    else
      System.out.println("The element popped out is : " + poppedElement + "\n");

    poppedElement = stack.pop();
    if (poppedElement == Integer.MIN_VALUE)
      System.out.println("Stack is empty\n");
    else
      System.out.println("The element popped out is : " + poppedElement + "\n");

    poppedElement = stack.pop();
    if (poppedElement == Integer.MIN_VALUE)
      System.out.println("Stack is empty\n");
    else
      System.out.println("The element popped out is : " + poppedElement + "\n");

    poppedElement = stack.pop();
    if (poppedElement == Integer.MIN_VALUE)
      System.out.println("Stack is empty\n");
    else
      System.out.println("The element popped out is : " + poppedElement + "\n");

    poppedElement = stack.pop();
    if (poppedElement == Integer.MIN_VALUE)
      System.out.println("Stack is empty\n");
    else
      System.out.println("The element popped out is : " + poppedElement + "\n");

    poppedElement = stack.pop();
    if (poppedElement == Integer.MIN_VALUE)
      System.out.println("Stack is empty\n");
    else
      System.out.println("The element popped out is : " + poppedElement + "\n");
  }
}