Showing posts with label Simple Stack Program In Java. Show all posts
Showing posts with label Simple Stack Program In Java. Show all posts

Sunday, December 22, 2013

Simple Example Program For Stack in Java Stack Utils

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

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