Stack In A Simple Way Example Java 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.

Stack In A Simple Way Example Program

import java.util.Stack;

public class SimpleStack {

    public static void main(String[] args) {
        Stack stack = new Stack();

        System.out.println("Stack Items \n" + stack);
        System.out.println("Stack Size :" + stack.size());

        System.out.println("Stack Push \n");
        stack.push("A");
        stack.push(new Integer(20));
        stack.push("Example");

        System.out.println("Stack Items \n" + stack);
        System.out.println("Stack Size :" + stack.size());

        System.out.println("Stack Pop \n");
        System.out.println("Pop Data :" + stack.pop());
        System.out.println("Pop Data " + stack.pop());
        System.out.println("Pop Data " + stack.pop());

        System.out.println("Stack Items \n" + stack);
        System.out.println("Stack Size :" + stack.size());
    }
}

Sample Output

Output is:
Stack Items 
[]
Stack Size :0
Stack Push 

Stack Items 
[A, 20, Example]
Stack Size :3
Stack Pop 

Pop Data :Example
Pop Data 20
Pop Data A
Stack Items 
[]
Stack Size :0