The current post contains the custom implementation of Stack ADT using Array in Java. StackArray class is the reference of Stack implementation using Array with the fixed size elements.
- push(): is used to store elements in Stack. It will throw StackOverflowError, if stack is full.
- pop(): is used to remove top element from Stack. It will throw EmptyStackException, if Stack if empty.
- peek(): is used to get top most element from Stack. It will throw EmptyStackException, if Stack if empty.
- search(): is used to find the element is present or not in Stack. It will throw EmptyStackException, if Stack if empty.
- show(): is used to print total elements present in Stack. It will throw EmptyStackException, if Stack if empty.
- isEmpty(): is used to find the Stack is empty or not. It will return true/false based on Stack.
- isFull(): is used to find the Stack is full or not. It will return true/false based on Stack.
- size(): is used to fine the Stack size.
package com.dsalgorithms.stack;
import java.util.EmptyStackException;
/*
* Author : Subbareddy
*
* StackArray class is the reference of Stack implementation using Array in Java.
*
*/
public class StackArray {
private int size;
private int index = 0;
private int[] ar;
/*
* StackArray constructor is used to create an array with passing size
*
* Input: integer value
*/
public StackArray(int size){
this.size = size;
ar = new int[size];
}
/*
* push() used to insert element into the stack
*
* Input: integer value
*
* Throws StackOverflowError error if stack is full
*/
public void push(int element){
if(isFull())
throw new StackOverflowError();
ar[index] = element;
index++;
}
/*
* pop() method removes the top most element and return the deleted element
*
* Throws EmptyStackException if Stack is empty
*/
public int pop(){
if(isEmpty())
throw new EmptyStackException();
return ar[--index];
}
/*
* peek() method returns the top most element in the Stack
*
* Throws EmptyStackException exception if Stack is empty
*/
public int peek(){
if(isEmpty())
throw new EmptyStackException();
return ar[index];
}
/*
* search() method verifies the element is present or not in the Stack
*
* Input: integer value
*
* Throws EmptyStackException exception if Stack is empty
*/
public int search(int element){
if(isEmpty())
throw new EmptyStackException();
for(int i=0;i<index;i++){
if(ar[i] == element)
return ar[i-1];
}
return -1;
}
/*
* show() method will print Stack elements
*
* Throws EmptyStackException exception if Stack is empty
*/
public void show(){
if(isEmpty())
throw new EmptyStackException();
for(int i=0;i<index;i++){
System.out.println(ar[i]);
}
}
/*
* isEmpty() method validates whether the Stack is empty or not
*
* Throws EmptyStackException exception if Stack is empty
*/
public boolean isEmpty(){
if(index == 0)
return true;
return false;
}
/*
* isFull() method will return true or false based on Stack is full or not
*/
public boolean isFull(){
if(index == size)
return true;
return false;
}
/*
* size() method will return total stack size
*/
public int size(){
return index;
}
public static void main(String[] args) {
//Creating an Empty stack with the size of 10
StackArray stack = new StackArray(10);
System.out.println("Initial Size of Stack is: "+stack.size());
//Pushing 1 to 10 integer type elements into Stack
System.out.println("\nPUSH 1-10 VALUES INTO STACK");
for(int i=0;i<10;i++){
stack.push(i+1);
}
//Printing the Stack size after pushing elements into Stack
System.out.println("\nAfter PUSH stack size: "+stack.size());
//Printing total number of elements from Stack
stack.show();
//Remove/Pop top 5 elements from Stack
System.out.println("\nPOP 5 elements from Stack");
for(int i=0;i<5;i++){
System.out.println(stack.pop());
}
//Printing total elements after performing pop
stack.show();
//Searching for specific element from Stack
int position = stack.search(2);
if(position == -1)
System.out.println("\nElement not present in stack");
else
System.out.println("\nElement located in the position of "+position);
}
}