Java Stack Implementation using Array

This tutorial gives an example of implementing a Stack data structure using an Array. The stack offers to put new objects on the stack (push) and to get objects from the stack (pop). A stack returns the object according to last-in-first-out (LIFO). Please note that JDK provides a default java stack implementation as class java.util.Stack.

The three mandatory operations applicable to all stack implementations are:

  • push(): a data item is placed at the location pointed to by the stack pointer.
  • pop(): a data item is removed and returned from the location pointed to by the stack pointer.

Some environments that rely heavily on stacks may provide additional operations as per requirements. The most common uses of Stack are expression evaluation and syntax parsing. We can find the usage of Stack in undo\redo operations in word processors as well.

1. Stack Implementation using Array

The following program is a sample implementation of Stack data structure. Feel free to modify the source code as per your need.

import java.util.Arrays;

public class Stack {

  private int maxSize;
  private Object[] stackArray;
  private int top;

  public Stack(int size) {
    maxSize = size;
    stackArray = new Object[maxSize];
    top = -1;
  }

  public void push(Object value) {
    if (isFull()) {
      System.out.println("Stack is full. Cannot push element.");
      return;
    }
    top++;
    stackArray[top] = value;
  }

  public Object pop() {
    if (isEmpty()) {
      System.out.println("Stack is empty. Cannot pop element.");
      return -1;
    }
    int oldTop = top;
    top--;
    stackArray[oldTop] = null;
    return stackArray[oldTop];
  }

  public Object peek() {
    if (isEmpty()) {
      System.out.println("Stack is empty. Cannot peek element.");
      return -1;
    }
    return stackArray[top];
  }

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

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

  @Override
  public String toString() {
    return "Stack: " + Arrays.toString(stackArray);
  }
}

2. Test

Now let’s test our Stack implementation by pushing and popping some objects from the stack.

We are creating a Stack of size 5. It will allow max 5 objects at any point in time. If we add a new object without removing an object first, it will give the error “Stack is full. Cannot push element.“.

Only after popping an object, we can add another object to Stack.

Stack stack = new Stack(5);

stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);

System.out.println(stack);

stack.push(6);   //Stack is full. Cannot push element.

stack.pop();

System.out.println(stack);

stack.push(6);

System.out.println(stack);

The program output:

Stack: [1, 2, 3, 4, 5]

Stack is full. Cannot push element.

Stack: [1, 2, 3, 4, null]

Stack: [1, 2, 3, 4, 6]

That’s all for this simple yet important concept about Java Stack Implementation example.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode