English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java Input Output (I/O)

Java Reader/Writer

Other Java topics

Java program to implement the (Stack) data structure

    Comprehensive Java Examples

In this example, we will learn to implement the stack data structure using Java.

To understand this example, make sure you first visit the following tutorials:

Example1: Java program to implement a stack

// Stack implementation in Java
class Stack {
  //Stores stack elements
  private int arr[];
  //Represents the top of the stack
  private int top;
  //The total capacity of the stack
  private int capacity;
  //Create a stack
  Stack(int size) {
    // Initialize the array
    // Initialize stack variables
    arr = new int[size];
    capacity = size;
    top = -1;
  }
  // Push an element to the top of the stack
  public void push(int x) {
    if (isFull()) {
      System.out.println("Stack OverFlow");
      // Terminate the program
      System.exit(1);
    }
    //Insert an element at the top of the stack
    System.out.println("Insert " + x);
    arr[++top] = x;
  }
  //Pop an element from the top of the stack
  public int pop() {
    //If the stack is empty
    //No elements to pop
    if (isEmpty()) {
      System.out.println("STACK EMPTY");
      //Terminate the program
      System.exit(1);
    }
    //Pop an element from the top of the stack
    return arr[top--];
  }
  //Return the size of the stack
  public int getSize() {
    return top + 1;
  }
  // Check if the stack is empty
  public Boolean isEmpty() {
    return top == -1;
  }
  // Check if the stack is full
  public Boolean isFull() {
    return top == capacity - 1;
  }
  // Display the elements of the stack
  public void printStack() {
    for (int i = 0; i <= top;++) {
      System.out.print(arr[i] + ", ");
    }
  }
  public static void main(String[] args) {
    Stack stack = new Stack(5);
    stack.push(1);
    stack.push(2);
    stack.push(3);
    System.out.print("Stack: ");
    stack.printStack();
    //Remove an element from the stack
    stack.pop();
    System.out.println("\nAfter popping");
    stack.printStack();
  }
}

Output Result

Insert 1
Insert 2
Insert 3
Stack: 1, 2, 3,  
After popping
1, 2,

In the above example, we have implemented the stack data structure using Java.

Example2: Implementing a stack using the Stack class

Java provides a built-in class Stack that can be used to implement a stack.

import java.util.Stack;
class Main {
  public static void main(String[] args) {
    //Create an object of the Stack class
    Stack<String> animals = new Stack<>();
    //Push an element onto the top of the stack
    animals.push("Dog");
    animals.push("Horse");
    animals.push("Cat");
    System.out.println("Stack: ", + animals);
    //Pop an element from the top of the stack
    animals.pop();
    System.out.println("After popping, Stack: ", + animals);
    }
}

Output Result

Stack: [Dog, Horse, Cat]
Stack: [Dog, Horse]

In the above example, we used Java's Stack class to implement the stack. Here,

  • animals.push() - Insert an element at the top of the stack

  • animals.pop() - Remove an element from the top of the stack

Note that we use <> when creating a stack. It indicates that the stack is a generic type. For more information about generics, please visitJava Generics.

Comprehensive Java Examples