DEV Community

Rahul kumar
Rahul kumar

Posted on

Implementing stack in JAVA.

Implementing stack in java

package com.hello;
import java.util.*;

class Stack{
    private int ar[];
    private int top;

    Stack(int size){
        this.ar = new int[size];
        this.top = -1;
    }

    /**
     * Get the top element of the stack.
     * You must have to check if stack is empty or not, before calling this API.
     * @return {Integer} top of the stack
     */
    public int top() {
        return this.ar[this.top];
    }

    /**
     * Check if stack is empty or not.
     * @return {Boolean} true if stack is empty, false otherwise
     */
    public boolean isEmpty() {
        return this.top == -1;
    }

    /**
     * Get the size of the stack
     * @return {Integer} size of the stack
     */
    public int size() {
        return this.top+1;
    }

    /**
     * Push el onto the top of the stack
     * @param el element to be pushed
     */
    public void push(int el) {
        if(this.top+1 >= this.ar.length) {
            System.out.println("Stack is full!");
            return;
        }

        // push el into stack
        this.ar[++this.top] = el;
    }

    /**
     * Pop the top element from the stack
     */
    public void pop() {
        if(this.top<0) {
            System.out.println("Stack is empty!");
            return;
        }

        this.top--;
    }
}
public class Method {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter stack size: ");
        int stackSize = sc.nextInt();
        System.out.println();

        Stack stack = new Stack(stackSize);

        int op = 1;
        System.out.println("Push: 1");
        System.out.println("Pop: 2");
        System.out.println("Top: 3");
        System.out.println("Size: 4");
        System.out.println("Exit: 0");
        while(op != 0) {
            System.out.print("Enter operation number: ");

            op = sc.nextInt();
            System.out.println();

            switch(op) {
                case 1:
                    System.out.print("Enter a number: ");
                    int el = sc.nextInt();
                    System.out.println();
                    stack.push(el);
                    break;
                case 2:
                    stack.pop();
                    break;
                case 3:
                    // if stack is empty then don't do pop()
                    if(stack.isEmpty()) {
                        System.out.println("Stack is empty!");
                        break;
                    }

                    int top = stack.top();
                    System.out.println("Top element: "+top);
                    break;
                case 4:
                    int size = stack.size();
                    System.out.println("Size: "+size);
                    break;
            }
        }

        // close the stream
        sc.close();
    }

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)