Java Program to Implement Stack Data Structure Using Arrays

In the previous article, we have seen Java Program to Create an Array and Fill it with Random Integer Values

In this article we are going to see how to Implement Stack data structure using Java programming language.

Java Program to Implement Stack Data Structure Using Arrays

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Program:

/* Java program to implement basic stack
operations */
// Driver code
class Main 
{
    public static void main(String args[]) 
    {
        Stack s = new Stack();
        s.push(10);
        s.push(20);
        s.push(30);
        System.out.println(s.pop() + " Popped from stack");
        System.out.println("Top element is: " + s.peek());
        System.out.print("Elements present in stack: ");
        s.print();
    }
}
class Stack 
{
    static final int MAX = 100;
    int top;
    int arr[] = new int[MAX]; // Maximum size of Stack

    boolean isEmpty()
    {
        return (top < 0);
    }

    Stack() 
    {
        top = -1;
    }

    boolean push(int x) 
    {
        if (top >= (MAX - 1)) 
        {
            System.out.println("Stack Overflow");
            return false;
        }
        else 
        {
            arr[++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 = arr[top--];
            return x;
        }
    }

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

    void print() 
    {
        for (int i = top; i > -1; i--) 
        {
            System.out.print(" " + arr[i]);
        }
    }
}


Output:

10 pushed into stack
20 pushed into stack
30 pushed into stack
30 Popped from stack
Top element is: 20
Elements present in stack: 20 10

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: