Java Stack Implementation using Array

Java Stack Implementation using Array: Here is the post which explains the implementation of the most useful data structure i.e. Stack using Array. First of all, we need to know what actually Stack is and what is its basic operations? So, let’s know about Stack.

Stack: A Stack is an abstract data type that follows a certain principle/rule for its set of operations i.e. insertion, deletion, search, etc. The principle followed by the stack is commonly known as LIFO (Last In First Out) or FILO (First In Last Out). i.e. the set of elements inserted first into the stack will be out at the last and the elements inserted last into the stack will be out first.

Java Stack Implementation using Array

Best real Life example to understand Stack: 

  1. A pile of plates in a canteen. The plates are stacked over one another using the LIFO order. The plate kept at first will be used and the plate kept at the bottom will be used at last.
  2. Undo and Redo process in the computer system: The process of undoing will fetch the last done activity from the Activity Stack and implement it similarly as Stack does through the principle of LIFO or FILO.
Stack Operations:
Basic Operations Descriptions of the Operations
 Push Insert a new element on the top of the stack. It throws  Overflow error if the stack is full.
 Pop Removes the topmost element from the stack. It throws Underflow error if the stack is Empty.
 Peek Returns the topmost element of the stack.
 Contains Check if the stack contains the specified element.
 isFull Check if the stack is full i.e. Stack’s capacity is equal to the total size of the stack.
 isEmpty Check if the stack is Empty.
 Size Returns the total size of the stack array.

Implementation of Stack using Array in Java: 

Java
package stacks;

//Generic Stack Implementation using Array 

public class StackArray < T > {

  //Default capacity of the Stack Array 
  int DEFAULT_CAPACITY = 10;
  int ACTUAL_CAPACITY;
  T[] array;
  int top = -1;

  //Default Constructor 
  @SuppressWarnings("unchecked")
  public StackArray() {
    this.ACTUAL_CAPACITY = this.DEFAULT_CAPACITY;
    array = (T[]) new Object[this.ACTUAL_CAPACITY];
  }

  //Single parameter constructor 

  @SuppressWarnings("unchecked")
  public StackArray(int capacity) {
    this.ACTUAL_CAPACITY = capacity - 1;
    array = (T[]) new Object[this.ACTUAL_CAPACITY];
  }

  //Push operation of the Stack 
  public void push(T value) {
    if (!isFull()) {
      array[++this.top] = value;
    } else {
      System.out.println("OverFlow! Stack Array is Full");
    }
  }

  //Pop operation of the Stack 
  public void pop() {
    if (!isEmpty()) {
      T val = array[this.top--];
      System.out.println("The popped element is: " + val);
    } else {
      System.out.println("UnderFlow! Stack Array is Empty");
    }
  }

  //Peek operation of the Stack i.e. returns the First element of the Stack 
  public T peek() {
    if (!isEmpty()) {
      return array[this.top];
    } else {
      System.out.println("UnderFlow! Stack Array is Empty");
      return null;
    }
  }

  //Contains operation of the Stack 
  public boolean contains(T value) {
    for (int i = 0; i <= this.top; i++) {
      if (this.array[i].equals(value)) {
        return true;
      }
    }
    return false;
  }

  //Check if the stack is full 
  public boolean isFull() {
    return this.top == this.ACTUAL_CAPACITY;
  }

  //Check if the stack is empty 
  public boolean isEmpty() {
    return this.top == -1;
  }

  //Returns the total size of the Stack 
  public int size() {
    return this.top + 1;
  }
  @Override public String toString() {
    String stackArray = "[ ";
    for (int i = top; i >= 0; i--) {
      stackArray += this.array[i] + " ";
    }
    stackArray += "]";
    return stackArray;
  }

  //Main Function 
  public static void main(String[] args) {
    StackArray < Integer > s1 = new StackArray < Integer > ();
    s1.push(10);
    s1.push(20);
    s1.push(50);
    System.out.println("Stack" + s1);
    s1.pop();
    System.out.println("Stack" + s1);
    System.out.println("Peek: " + s1.peek());
    System.out.println("Total Size of the Stack: " + s1.size());
    System.out.println("Stack s1 contains element 20 ? " + s1.contains(20));
  }
}
Java
 

The output of the above code Implementation: 

Stack[ 50 20 10 ]
The popped element is: 50
Stack[ 20 10 ]
Peek: 20
Total Size of the Stack: 2
Stack s1 contains element 20 ? true

That’s it for now. Hope you understood the concepts of Java Stack Implementation using Array now.

Read more on Computer Programming articles:-

 Computer Programming Articles

Contribute to EduTechLearners:-

Please write comments if you want to share more information regarding the above notes. If you want some more notes/books on any of the topics please mail to us or comment below. We will provide you as soon as possible and if you want your’s notes to be published on our site then feel free to contribute on EduTechLearners or email your content to contribute@edutechlearners.com (The contents will be published by your Name).

Leave a Reply