Dremendo Tag Line

Stack in Java Programming

Data Structure in Java

In this lesson, we will understand what is Stack in Java Programming and how to create them along with some examples.

What is Stack in Java

A Stack in Java is a data structure in which we can insert or delete element only at the top of the stack.

We can see the example of a stack in our daily life as the stack of books.

c stack example

In the above image, we can see that we can take a book only from the top. Similarly, we can keep one more book only at the top.

video-poster

Operation on Stack

There are two operations possible on the stack.

  • Push - When we add an element in the stack.
  • Pop - When we delete an element from the stack.

To understand how the above operations work on a stack. See the example given below.

c stack using array

From the above image, we can see that the elements, which are added (push) last in the stack is deleted (pop) first from the stack.

The stack behaves like a last in first out manner. It means that when an element is added to a stack, it becomes the last element unless we add more and, when we remove an element from the stack, the last added element removed first.

So a stack is also known as LIFO (Last In First Out) data structure.

Implementation of Stack in Java

The stack in Java programming can be implemented in two ways using:

  • Array
  • Single Linked List

In this lesson, we will see the implementation of a stack using an array. We will also discuss stack using a single linked list later on in the subsequent lesson.

Array Implementation of Stack

Since a stack is a collection of the same type of elements, so we can implement the stack using an array.

c stack example 1

In the above image, we can see an array named arr in a vertical manner whose size is 5. We take a variable top, which keeps the position of the top element in the array.

Push Operation on Stack

For push operation on the stack first, we check if the value of top is equal to the value of size-1 then, we will display a message Stack is full, else we will increase the value of top by 1 and add the element in the array at the new location of top.

Example

if(top==size-1)
{
    System.out.println("Stack is full");
}
else
{
    top=top+1;
    arr[top]=new_item;
}

If we insert three elements, say 12, 15 and 26 to the stack, then the stack will look like as shown in the image below.

c stack example 2

Pop Operation on Stack

For pop operation on the stack first, we check if the value of top is equal to -1 then, we will display a message Stack is empty, else we will display the popped element on the screen and then decrease the value of top by 1.

Example

if(top==-1)
{
    System.out.println("Stack is empty");
}
else
{
    System.out.println("Element Popped = " + arr[top]);
    top=top-1;
}

If we delete the last elements 26 from the stack, then the stack will look like as shown in the image below.

c stack example 3

Program of Stack using Array

Below is the complete program of stack in Java using an array having size 5.

Stack Program in Java using Array

import java.util.Scanner;

public class Example
{
    public static void main(String args[])
    {
        int size=5;
        int arr[]=new int[size],top=-1,ch,n,i;
        Scanner sc=new Scanner(System.in);

        for(;;)		// An infinite loop
        {
            System.out.println("\n1. Push");
            System.out.println("2. Pop");
            System.out.println("3. Display");
            System.out.println("4. Exit");
            System.out.print("Enter Choice: ");
            ch=sc.nextInt();

            switch(ch)
            {
                case 1:
                    if(top==size-1)
                    {
                        System.out.println("Stack is full");
                    }
                    else
                    {
                        System.out.print("Enter a number ");
                        n=sc.nextInt();
                        top++;
                        arr[top]=n;
                    }
                    break;

                case 2:
                    if(top==-1)
                    {
                        System.out.println("Stack is empty");
                    }
                    else
                    {
                        System.out.println("Number Popped = " + arr[top]);
                        top--;
                    }
                    break;

                case 3:
                    if(top==-1)
                    {
                        System.out.println("Stack is empty");
                    }
                    else
                    {
                        for(i=top; i>=0; i--)
                        {
                            System.out.println(arr[i]);
                        }
                    }
                    break;

                case 4:
                    System.exit(0);
                    break;

                default:
                    System.out.println("Wrong Choice");
            }
        }
    }
}