Simple Example Program For Stack in Java Stack Utils

Logo

Stack Using Java Stack Utils Java Example Program

This page contains simple Java example programs for Stack Using Java Stack Utils Java Example Program with sample output. This java example program also expain the concepts for clearly.

Go to Program

Definition:

A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.

Simple Example Program For Stack in Java Stack Utils

// Simple Example Program For Stack in Java Stack Utils
// Coded By Thiyagaraaj M.P

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

class StackProgram {

    public static void main(String[] args) throws IOException {
        Stack stk = new Stack();
        stk.setSize(5);
        
        boolean yes=true;
        int choice;
        BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
        
        do{
            System.out.println("1).Push\n2).Pop\n3).Exit\n\nEnter Choice");
            choice = Integer.parseInt(is.readLine());
            
            switch(choice)
            {
                case 1: System.out.println("Enter Push Item: ");
                        stk.push(Integer.parseInt(is.readLine()));
                        break;
                case 2: System.out.println("Poped Item : "+stk.pop());break;
                case 3: yes = false;break;
                default: System.out.println("Invalid Choice");
            }
        }while(yes==true);
        
    }
}

Sample Output:

run:
1).Push
2).Pop
3).Exit

Enter Choice
1
Enter Push Item: 
23
1).Push
2).Pop
3).Exit

Enter Choice
1
Enter Push Item: 
45
1).Push
2).Pop
3).Exit

Enter Choice
2
Poped Item : 45
1).Push
2).Pop
3).Exit

Enter Choice
2
Poped Item : 23
1).Push
2).Pop
3).Exit