How to implement Stack Data Structure in Java Program

Java Program to Implement Stack Data structure

Stack Data Structure 

An array or linked list can be used to implement a stack in Java. In the java.util package, Java includes a pre-implemented Stack class that extends the Vector class and implements the Stack interface.

  • Push operation adds an element to the top of the stack
  • Pop operation removes an element from the top of the stack

To know more about Java Program to Implement Stack Data Structure  read the complete article.

Steps to Implement Stack Data Structure in Java Program

  • Here are the steps to implement a stack data structure in Java:
    • Create a class “Stack” to define the stack data structure.
    • Define an array of integers or any other data type to store the elements in the stack.
    • Create a variable “top” to keep track of the top element of the stack.
    • Implement the push() method to add elements to the top of the stack.
    • Implement the pop() method to remove elements from the top of the stack.
    • Implement the isEmpty() method to check if the stack is empty.
    • Implement the peek() method to get the top element of the stack without removing it.
  • In the main method, create an object of the Stack class and test the methods by pushing and popping elements from the stack.

Let’s look at a Java Program to Implement Stack Data Structure to perform certain operations.

Example 1: Java Program to Implement Stack Data Structure

Run
class Stack {
  int top;
  int[] stack = new int[100];

  Stack() {
    top = -1;
  }

  public void push(int x) {
    stack[++top] = x;
  }

  public int pop() {
    return stack[top--];
  }

  public boolean isEmpty() {
    return top == -1;
  }

  public int peek() {
    return stack[top];
  }
}

public class Main {
  public static void main(String[] args) {
    Stack stack = new Stack();
    stack.push(1);
    stack.push(2);
    stack.push(3);
    while (!stack.isEmpty()) {
      System.out.println(stack.pop());
    }
  }
}

Output

3
2
1

Example 2 : Java Program to Implement Stack

Run
import java.util.ArrayList;


class Stack {
    private ArrayList list = new ArrayList();
    
    public void push(T element) {
        list.add(element);
    }
    
    public T pop() {
        if (list.isEmpty()) {
            throw new IllegalStateException("Stack is empty");
        }
        return list.remove(list.size() - 1);
    }
    
    public boolean isEmpty() {
        return list.isEmpty();
    }
    
    public T peek() {
        if (list.isEmpty()) {
            throw new IllegalStateException("Stack is empty");
        }
        return list.get(list.size() - 1);
    }
        
}
public class Main {
     public static void main(String[] args) {
        Stack stack = new Stack<>();
        
        stack.push(1);
        stack.push(2);
        stack.push(3);
        
        System.out.println("Peek: " + stack.peek());
        System.out.println("Pop: " + stack.pop());
        System.out.println("Pop: " + stack.pop());
        System.out.println("Is Empty: " + stack.isEmpty());
        System.out.println("Pop: " + stack.pop());
        System.out.println("Is Empty: " + stack.isEmpty());
    }
}

Output

Peek: 3
Pop: 3
Pop: 2
Is Empty: false
Pop: 1
Is Empty: true

Example 3: Java Program to sort a map by Values.

Run
import java.util.Stack;

class Main {
    public static void main(String[] args) {
        Stack integerStack = new Stack<>();
        // Pushing 5 element into the stack
        integerStack.push(79);
        integerStack.push(9);
        integerStack.push(7);
        integerStack.push(7979);
        integerStack.push(5);

        // printing the stack's elements
        System.out.println("Elements in the Stack :: " + integerStack);

        // Pop operation on the stack, 2 would be removed from the top
        integerStack.pop();

        // printing the stack's elements
        System.out.println("Elements in the Stack :: " + integerStack);

        // Peek operation on the stack,100 would be printed
        System.out.println("Top of the stack :: " + integerStack.peek());
    }
}

Output

Elements in the Stack :: [79, 9, 7, 7979, 5]
Elements in the Stack :: [79, 9, 7, 7979]
Top of the stack :: 7979

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription