Stack Implementation using Array in Java

0
515
Advertisement

In the previous post, we talked about the Stack Data Structure and its implementation in LinkedList. you can read that post here.  And, in this post, we are going to implement the Stack data structure using Array in Java.

Stack Implementation using Array:

Array Implementation of Stack contains 2 classes.Class 1 ie; StackArray.java have the Stack implementation and its method like push(), pop() and display(). Class 2 ie; StackArrayImplementation.java has implemented StackArray.java and provide the implementation for Stack using Array.

StackArray.java

public class StackArray {

    int STACK_SIZE;

    int[] stack;

    public StackArray(int size){
        this.STACK_SIZE = size;
        this.stack = new int[size];
    }


    public int push(int top,int ele){
        if(top == STACK_SIZE-1){
            System.out.println("Stack Overflow.");
        }else {
            top++;
            stack[top] = ele;
        }
        return top;
    }

    public int pop(int top){
        if (top == -1){
            System.out.println("Stack Underflow");
        }else{
            System.out.println(String.format("%d removed from the Stack.",stack[top--]));
        }

        return top;
    }

    public void display(int top){
        if (top == -1){
            System.out.println("Stack is Empty.");
        }else{
            while(top>0){
                System.out.print(String.format("%d--->",stack[top]));
                top--;
            }
            System.out.print(String.format("%d--->NULL",stack[top]));
        }
    }


    public int stackSize(int top){
        int count = 0;
        int temp = top;
        if (temp == -1){
            return count;
        }else{
            while (temp>= 0){
                ++count;
                temp--;
            }
        }
        return count;
    }
}

StackArrayImplementation:

import java.util.Scanner;

public class StackArrayImplementation {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the Size of the Stack : ");
        int n = scanner.nextInt();

        System.out.println("=================================");

        //int[] stack = new int[n];

        StackArray stack = new StackArray(n);


        int top = -1;

        System.out.println("Enter the Stack elements : ");

        for (int i = 0;i<n;i++){
            int ele = scanner.nextInt();
            top = stack.push(top,ele);
        }

        System.out.println("=================================");

        System.out.println("Stack after Inserting the elements : ");
        stack.display(top);

        System.out.println();
        System.out.println("=================================");

        System.out.println("Stack Size after inserting the elements : ");
        System.out.print(stack.stackSize(top));

        System.out.println();

        System.out.println("=================================");


        System.out.println("After applying the pop() operation :");
        top = stack.pop(top);

        System.out.println("=================================");

        System.out.println("Stack after 1st pop() operation performed : ");
        stack.display(top);

        System.out.println();

        System.out.println("=================================");

        System.out.println("Stack Size after 1st pop() : ");
        System.out.print(stack.stackSize(top));
        System.out.println();

        System.out.println("=================================");

    }
}

 

Output 1:

Enter the Size of the Stack : 
5
=================================
Enter the Stack elements : 
2
4
5
7
8
=================================
Stack after Inserting the elements : 
8--->7--->5--->4--->2--->NULL
=================================
Stack Size after inserting the elements : 
5
=================================
After applying the pop() operation :
8 removed from the Stack.
=================================
Stack after 1st pop() operation performed : 
7--->5--->4--->2--->NULL
=================================
Stack Size after 1st pop() : 
4
=================================

 

Output 2:

Enter the Size of the Stack : 
0
=================================
Enter the Stack elements : 
=================================
Stack after Inserting the elements : 
Stack is Empty.

=================================
Stack Size after inserting the elements : 
0
=================================
After applying the pop() operation :
Stack Underflow
=================================
Stack after 1st pop() operation performed : 
Stack is Empty.

=================================
Stack Size after 1st pop() : 
0
=================================

 

Hope you like this post.

If you have any doubt please feel free to comment below.

Learn Stack Implementation using LinkedList

 

You may also learn these to build the Knowledge:

LEAVE A REPLY

Please enter your comment!
Please enter your name here