Java Program to Implement Stack

This is a Java Program to implement a stack. Stack is an area of memory that holds all local variables and parameters used by any function and remembers the order in which functions are called so that function returns occur correctly. ‘push’ operation is used to add an element to stack and ‘pop’ operation is used to remove an element from stack. ‘peek’ operation is also implemented returning the value of the top element without removing it. The relation between the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data structure. The implemented stack has bounded capacity.

Here is the source code of the Java program to implement a stack. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 * Java Program to Implement Stack
 */
 
import java.util.*;
 
/*  Class arrayStack  */
class arrayStack
{
    protected int arr[];
    protected int top, size, len;
    /*  Constructor for arrayStack */
    public arrayStack(int n)
    {
        size = n;
        len = 0;
        arr = new int[size];
        top = -1;
    }
    /*  Function to check if stack is empty */
    public boolean isEmpty()
    {
        return top == -1;
    }
    /*  Function to check if stack is full */
    public boolean isFull()
    {
        return top == size -1 ;        
    }
    /*  Function to get the size of the stack */
    public int getSize()
    {
        return len ;
    }
    /*  Function to check the top element of the stack */
    public int peek()
    {
        if( isEmpty() )
            throw new NoSuchElementException("Underflow Exception");
        return arr[top];
    }
    /*  Function to add an element to the stack */
    public void push(int i)
    {
        if(top + 1 >= size)
            throw new IndexOutOfBoundsException("Overflow Exception");
        if(top + 1 < size )
            arr[++top] = i;
        len++ ;
    }
    /*  Function to delete an element from the stack */
    public int pop()
    {
        if( isEmpty() )
            throw new NoSuchElementException("Underflow Exception");
        len-- ;
        return arr[top--]; 
    }    
    /*  Function to display the status of the stack */
    public void display()
    {
        System.out.print("\nStack = ");
        if (len == 0)
        {
            System.out.print("Empty\n");
            return ;
        }
        for (int i = top; i >= 0; i--)
            System.out.print(arr[i]+" ");
        System.out.println();
    }    
}
 
/*  Class StackImplement  */
public class StackImplement
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);        
        System.out.println("Stack Test\n");
        System.out.println("Enter Size of Integer Stack ");
        int n = scan.nextInt();
        /* Creating object of class arrayStack */
        arrayStack stk = new arrayStack(n);
        /* Perform Stack Operations */
        char ch;
        do{
            System.out.println("\nStack Operations");
            System.out.println("1. push");
            System.out.println("2. pop");
            System.out.println("3. peek");
            System.out.println("4. check empty");
            System.out.println("5. check full");
            System.out.println("6. size");
            int choice = scan.nextInt();
            switch (choice)
            {
            case 1 : 
                System.out.println("Enter integer element to push");
                try 
                {
                    stk.push( scan.nextInt() );
                }
                catch (Exception e)
                {
                    System.out.println("Error : " + e.getMessage());
                }                         
                break;                         
            case 2 : 
                try
                {
                    System.out.println("Popped Element = " + stk.pop());
                }
                catch (Exception e)
                {
                    System.out.println("Error : " + e.getMessage());
                }    
                break;                         
            case 3 :         
                try
                {
                    System.out.println("Peek Element = " + stk.peek());
                }
                catch (Exception e)
                {
                    System.out.println("Error : " + e.getMessage());
                }
                break;                         
            case 4 : 
                System.out.println("Empty status = " + stk.isEmpty());
                break;                
            case 5 :
                System.out.println("Full status = " + stk.isFull());
                break;                 
            case 6 : 
                System.out.println("Size = " + stk.getSize());
                break;                         
            default : 
                System.out.println("Wrong Entry \n ");
                break;
            }
            /* display stack */
            stk.display();            
            System.out.println("\nDo you want to continue (Type y or n) \n");
            ch = scan.next().charAt(0);
 
        } while (ch == 'Y'|| ch == 'y');                 
    }
}
Stack Test
 
Enter Size of Integer Stack
5
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
4
Empty status = true
 
Stack = Empty
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
1
Enter integer element to push
24
 
Stack = 24
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
1
Enter integer element to push
6
 
Stack = 6 24
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
1
Enter integer element to push
162
 
Stack = 162 6 24
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
1
Enter integer element to push
19
 
Stack = 19 162 6 24
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
1
Enter integer element to push
94
 
Stack = 94 19 162 6 24
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
5
Full status = true
 
Stack = 94 19 162 6 24
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
1
Enter integer element to push
32
Error : Overflow Exception
 
Stack = 94 19 162 6 24
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
3
Peek Element = 94
 
Stack = 94 19 162 6 24
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
2
Popped Element = 94
 
Stack = 19 162 6 24
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
2
Popped Element = 19
 
Stack = 162 6 24
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
2
Popped Element = 162
 
Stack = 6 24
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
6
Size = 2
 
Stack = 6 24
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
2
Popped Element = 6
 
Stack = 24
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
2
Popped Element = 24
 
Stack = Empty
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
2
Error : Underflow Exception
 
Stack = Empty
 
Do you want to continue (Type y or n)
 
y
 
Stack Operations
1. push
2. pop
3. peek
4. check empty
5. check full
6. size
 
4
Empty status = true
 
Stack = Empty
 
Do you want to continue (Type y or n)
 
n

Related posts:

Lập trình đa luồng với Callable và Future trong Java
Java Program to Search for an Element in a Binary Search Tree
Assertions in JUnit 4 and JUnit 5
Java Program to Implement Affine Cipher
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Test a REST API with Java
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Spring Boot - Unit Test Cases
Guide to Guava Multimap
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Add Multiple Items to an Java ArrayList
Hướng dẫn Java Design Pattern – MVC
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Read an Outlook MSG file
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
A Guide to Java HashMap
Biểu thức Lambda trong Java 8 – Lambda Expressions
Java Program to Implement Red Black Tree
Introduction to the Functional Web Framework in Spring 5
Java Program to Implement Min Hash
How to Break from Java Stream forEach
DynamoDB in a Spring Boot Application Using Spring Data
Debugging Reactive Streams in Java
Hướng dẫn sử dụng Printing Service trong Java
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Guide to ThreadLocalRandom in Java
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Java Program to Generate Random Numbers Using Middle Square Method
Filtering and Transforming Collections in Guava