DEV Community

Cover image for Implementation Of Stacks Using Array In JAVA
Gourav Kadu
Gourav Kadu

Posted on

4 1

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

Code

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
        if(top <= max_size) {
        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");
            top--;
        }   
    }


    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 void Top()
    {
         System.out.println(arr[top] +" at index "+ top);
    }


    public static void main(String[] args)
    {

        Template t = new Template();
        t.Stack(4);
        t.push(6);
        t.Top(); // <---- Return Top variable of Stack
        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);
        t.Top(); // <---- Return Top variable of Stack
        t.push(10);
        t.Top(); // <---- Return Top variable of Stack
        t.push(112);   
        t.Top();    // <---- Return Top variable of Stack
        t.push(14); //<----- stack will be full at this point
        t.Top();
        t.push(146); // so if you push anything it will print "OVERFLOW"
        t.push(110);
        t.Top(); // <---- Return Top variable of Stack
    }

}
Enter fullscreen mode Exit fullscreen mode

API Trace View

Struggling with slow API calls?

Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more