What is Stack and How to implement Stack in Java without Collection?

What is Stack in Java?

Have you heard of LIFO? Last-In, First-Out concept? Well, Stack is a LIFO implementation of linear Data Structure. That means, Objects can be inserted or removed from only one end OR in other words only from top.

Here is our own implementation of Stack in Java

We will create below functions for Java Stack. Please note here: We are not using any built in Java Collection class for Stack implementation.

We will use Java Collection for Stack in next tutorial. It’s out now 🙂 Article link.

  • crunchifyPush() – it inserts an item at the top of the stack.
  • crunchifyPop() – it removes the object at the top of the stack and returns that object from the function. The stack size will be decremented by one.
  • crunchifyPeek() – it returns the object at the top of the stack without removing it from the stack or modifying the stack in any way.
  • crunchifyIsEmpty() – it checks if the stack is empty or not.
  • crunchifyIsFull() – it tests if the stack is full or not.
  • crunchifySize() – it returns the total number of elements present in the stack.

Let’s get started:

package crunchify.com.java.tutorials;

/**
 * @author Crunchify.com
 * How to implement Stack in Java? Best way to implement Stack in Java.
 * Revision: 1.0
 */

public class CrunchifyJavaStackTutorial {
    private static int crunchifyStackSize = 0;
    private static long[] crunchifyStackArray = new long[0];
    private static int crunchifyStackTop;

    public CrunchifyJavaStackTutorial(int crunchifyInt) {
        crunchifyStackSize = crunchifyInt;
        crunchifyStackArray = new long[crunchifyStackSize];
        crunchifyStackTop = -1;
    }

    // Let's implement below standard Stack utilities
    /*
        crunchifyPush() - it inserts an item at the top of the stack.
        crunchifyPop() - it removes the object at the top of the stack and returns that object from the function. The stack size will be decremented by one.
        crunchifyPeek() - it returns the object at the top of the stack without removing it from the stack or modifying the stack in any way.
        crunchifyIsEmpty() - it checks if the stack is empty or not.
        crunchifyIsFull() - it tests if the stack is full or not.
        crunchifySize() - it returns the total number of elements present in the stack.
     */

    // crunchifyPop(): it removes the object at the top of the stack and returns that object from the function. The stack size will be decremented by one.
    public long crunchifyPop() {
        return crunchifyStackArray[crunchifyStackTop--];
    }

    // crunchifyPeek: it returns the object at the top of the stack without removing it from the stack or modifying the stack in any way.
    public static long crunchifyPeek() {
        return crunchifyStackArray[crunchifyStackTop];
    }

    // crunchifyIsEmpty - it checks if the stack is empty or not.
    public static boolean crunchifyIsEmpty() {
        return (crunchifyStackTop == -1);
    }

    // crunchifyIsFull() - it tests if the stack is full or not.
    public static boolean crunchifyIsFull() {
        return (crunchifyStackTop == crunchifyStackSize - 1);
    }

    // crunchifyPush() - it inserts an item at the top of the stack.
    public void crunchifyPush(long j) {
        crunchifyStackArray[++crunchifyStackTop] = j;
    }

    // crunchifySize() - it returns the total number of elements present in the stack.
    public static int crunchifySize() {
        return crunchifyStackTop + 1;
    }

    public static void main(String[] crunchifyArgs) {
        CrunchifyJavaStackTutorial crunchifyStack = new CrunchifyJavaStackTutorial(5);
        crunchifyPrint("Is Stack Empty? " + crunchifyIsEmpty() + "\n");

        crunchifyStack.crunchifyPush(123);
        crunchifyStack.crunchifyPush(234);
        crunchifyStack.crunchifyPush(345);
        crunchifyStack.crunchifyPush(456);
        crunchifyStack.crunchifyPush(567);

        crunchifyPrint("Peek() value: " + crunchifyPeek() + "\n");


        while (!crunchifyIsEmpty()) {
            long crunchifyValue = crunchifyStack.crunchifyPop();
            crunchifyPrint("Stack value: " + crunchifyValue);

        }
        crunchifyPrint("");
        crunchifyPrint("Is Stack Empty? " + crunchifyIsEmpty());
        crunchifyPrint("Is Stack Full? " + crunchifyIsFull() + "\n");

    }

    //  Simple Crunchify Print Utility
    private static void crunchifyPrint(Object crunchifyValue) {
        System.out.println(crunchifyValue);
    }
}

Run Java Program:

Just run above program as a Java Application and you should see result as below.

Is Stack Empty? true

Peek() value: 567

Stack value: 567
Stack value: 456
Stack value: 345
Stack value: 234
Stack value: 123

Is Stack Empty? true
Is Stack Full? false


Process finished with exit code 0

Let me know if you have any question or get any exception running above Java program and I’m more than happy to debug this with you.

Java Stack Implementation using Collection

The post What is Stack and How to implement Stack in Java without Collection? appeared first on Crunchify.