Sunday, December 22, 2013

Simple Example Program For Stack in Java( Simple Way Of Use )

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


// Simple Example Program For Stack ( Simple Way Of Use )

// Coded By Thiyagaraaj M.P

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:


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

No comments:

Post a Comment