3

Implementation Of Stacks Using Array In JAVA

 2 years ago
source link: https://dev.to/gouravmpk/implementation-of-stacks-using-array-in-java-599h
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Cover image for Implementation Of Stacks Using Array In JAVA
Gourav Kadu

Posted on Nov 2

Implementation Of Stacks Using Array In JAVA

Statement

I created an Stack using array and created its Three Basic operations i.e. PUSH , POP and Top.

where PUSH is insertion operation.
POP is deletion operation.
Top just returns variable at top of stack.
I also added Logic of "Overflow" and "Underflow" state with some exception handling.
The code is explained in details using comments .

To Tinker the inputs and check the operations Click Here

import java.util.*;

public class Template {
    static int max_size;
    static int top ;
    int temp;
    static int arr[];
    public void Stack(int size) {
         arr = new int[size];
         max_size = arr.length;
         top = -1;
    }

    public boolean empty() {          //checking if stack is empty .
        return temp  == -1;
    }

    public boolean full() {  //checking if stack is full .
        return top + 1 == max_size;
    }

    public void push(int Ip) { //PUSH operation 
        top++;
        try {                           //bit of exception handling while insertion/PUSH operation
            if(top >max_size) {
                throw new ArrayIndexOutOfBoundsException();       
            }
            else if(empty()|| top<=max_size) {
            arr[top] = Ip; 
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Stack overflow");
        }   
    }


    public void pop() {       // POP 
         temp = top;
    if(top > -1) {           // stop top from further decrementing if already -1,
        top--;              // if not it will interfere with push operation.
        }else { temp--;}        
    try {                       //bit of exception handling while deletion/POP operation
        if(temp < -1) throw new ArrayIndexOutOfBoundsException();
    }catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Stack underflow");  

    }
    }



    public static void main(String[] args)
    {

        Template t = new Template();
        t.Stack(4);
        t.push(6);
          System.out.println(arr[top]);
        t.pop(); // <--- 6 is removed from the stack so stack will be empty
        t.pop(); // <-- so removing element from empty stack will return UnderFlow
        t.push(101);
          System.out.println(arr[top]);
        t.push(10);
          System.out.println(arr[top]);
        t.push(112);   
          System.out.println(arr[top]);
        t.push(14); //<----- stack will be full at this point
          System.out.println(arr[top]);
        t.push(146); // so if you push anything it will print OverFlow

    }

}
Enter fullscreen modeExit fullscreen mode

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK