Implement Stack Data Structure in Java

Exercise:

Write a Java Program to implement stack data structure.

1.Implement stack

Click Here to View the Solution!
public class Stack {

    // store elements of stack
    private int arr[];

    //declare top and capacity of stack
    private int top ,capacity;
    Stack(int size) {
        
        // initialize the array with stack variables
        arr = new int[size];
        capacity = size;
        top = -1;
    }

    // push elements to the top of stack
    public void push(int x) {
        if (isFull()) {
            System.out.println("Stack OverFlow");
            System.exit(1);
        }

        // insert element on top of stack
        System.out.println("Inserting " + x);
        arr[++top] = x;
    }

    // pop elements from top of stack
    public int pop() {

        // if stack is empty
        if (isEmpty()) {
            System.out.println("STACK EMPTY");
            System.exit(1);
        }

        // return top of stack element
        return arr[top--];
    }

    // return size of the stack
    public int getSize() {
        return top + 1;
    }

    // check if the stack is empty
    public Boolean isEmpty() {
        return top == -1;
    }

    // check if the stack is full
    public Boolean isFull() {
        return top == capacity - 1;
    }

    // display elements of stack
    public void printStack() {
        for (int i = 0; i <= top; i++) {
            System.out.print(arr[i] + ", ");
        }
    }
    public static void main(String[] args) {
        Stack stack = new Stack(5);
        stack.push(11);
        stack.push(22);
        stack.push(33);
        stack.push(44);
        System.out.print("Stack: ");
        stack.printStack();

        // remove element from stack
        stack.pop();
        System.out.println("\nAfter popping out");
        stack.printStack();
    }
}
Click Here to View the Output!
 Inserting 11
 Inserting 22
 Inserting 33
 Inserting 44
 Stack: 11, 22, 33, 44, 
 After popping out
 11, 22, 33, 
Click Here to View the Explanation!
  • This program shows how to create a stack-type data structure.
  • Here we declare a class by the name of Stack, which contains three integer type private class members: an array to store stack values, top of stack, and capacity.
  • The constructor of Stack class takes an argument of integer type, which determines the size of the stack.
  • An array of this size is initialized, also this size is assigned to the capacity, and the top of stack is set to -1 initially.
  • Push method deals with insertion of the elements, passed in the argument; at the top as long as the stack does not over flow i.e. verified using isFull() method. Also top is incremented by 1.
  • The Pop method returns stack elements from the top; here top gets decremented.
  • If the stack is empty the top will be -1, and in case the stack is full top will be equal to the total size minus one.
  • In the main method, we inserted elements by calling push method, removed an element by calling pop method and then printed the stack by iterating over each element stored in the stack array.

2.Implement using stack class

Click Here to View the Solution!
import java.util.Stack;

public class Main {
    public static void main(String[] args) {

        // create an object of Stack class
        Stack<String> languages= new Stack<>();

        // push elements to top of stack
        languages.push("Java");
        languages.push("Javascript");
        languages.push("Swift");
        System.out.println("Stack: " + languages);

        // pop element from top of stack
        languages.pop();
        System.out.println("Stack after pop: " + languages);
    }
}
Click Here to View the Output!
Stack: [Java, Javascript, Swift]
Stack after pop: [Java, Javascript]
Click Here to View the Explanation!
  • This program shows how to create a stack-type data structure.
  • For implementing stack data structure, we use Java provided class named Stack.
  • In the main method, we instantiate an object of Stack class, i.e. of String type.
  • Notice the use of angular brackets for creating a Stack object.
  • To insert elements at the top of Stack, we call push() method provided by Stack class.
  • In the following example, three elements have been added to the Stack.
  • After that the Stack is printed, and it is of the form: [Java, Javascript, Swift].
  • Then we remove an element from the top, using pop() method that is also provided by Stack class.
  • After removing the element from the top, Stack looks something like this: [Java, Javascript].