Create a Stack Of Array in Java

Programming is always a fun and creative task. The more creative you can think the better programmer you will become. However, programming isn’t always about solving problems sometimes there’s also a fun aspect to it which makes it way more satisfying. If you are a programmer then you might have already played with patterns, strings, etc multiple times. Well, it’s also like that you are already familiar with arrays and numbers, so what about creating a stack. So in today’s article, we will Create a Stack of Array using Java. With the help of this program, you will be able to create and learn stack data structure. Practising these types of questions also helps to get an upper edge in Competitive Programming.

 

So open up your IDE and let’s get started real quick. Once you understand the logic practice this program on your own to make your brain work in a problem-solving way. This is a pretty easy question so you might not get asked in an interview but practising this question will make your grip more firm in the Java language.

 

Also Read: Print Duplicate Characters in String Using Java

 

What’s The Approach?

 

  • We will create an array of fixed size 1000.

 

  • If there’s no element in the array, the the stack will be called underflow and if we add element in the stack after exceeding it’s size then it’ll be called overflow.

 

  • The element added at the last will be removed first, if in any case we want to delete elements from the stack.

 

  • .push() method will be used to insert the elements in the stack, .pop() method will be used to remove an element from the stack.

 

Java Program To Create A Stack Of Array

 

Input : 

push(10)
push(20)
push(30)
pop()
/* Java program to implement basic stack
operations */
class TechDecodeTutorials {
    static final int MAX = 1000;
    int top;
    int a[] = new int[MAX]; // Maximum size of Stack

    boolean isEmpty()
    {
        return (top < 0);
    }
    TechDecodeTutorials()
    {
        top = -1;
    }

    boolean push(int x)
    {
        if (top >= (MAX - 1)) {
            System.out.println("Stack Overflow");
            return false;
        }
        else {
            a[++top] = x;
            System.out.println(x + " pushed into stack");
            return true;
        }
    }

    int pop()
    {
        if (top < 0) {
            System.out.println("Stack Underflow");
            return 0;
        }
        else {
            int x = a[top--];
            return x;
        }
    }

    int peek()
    {
        if (top < 0) {
            System.out.println("Stack Underflow");
            return 0;
        }
        else {
            int x = a[top];
            return x;
        }
    }
}

// Driver code
class Main {
    public static void main(String args[])
    {
        TechDecodeTutorials s = new TechDecodeTutorials();
        s.push(10);
        s.push(20);
        s.push(30);
        System.out.println(s.pop() + " Popped from stack");
    }
}

 

Output:
10 pushed into stack

20 pushed into stack

30 pushed into stack

30 Popped from stack

Ethix

I'm a coding geek interested in cyberspace who loves to write and read

Leave a Reply

Your email address will not be published.