Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Monday, 18 July 2011

Program to Implement Stack in Java


    This program is used to demonstrate the Stack operations PUSH & POP with the help of Switch an if statements.


import java.io.*;

import java.util.*;

class Stack

{

    int a[]=new int[10];

    int top=0;

    Stack()


    {

        for(int i=0; i<5 ; i++)

        {

            a[i]=0;

        }

    }

    void push()

    {

        if(top >= 4)

        {

            System.out.println("Stack full");

        }

        else

        {

            Scanner sc1=new Scanner(System.in);

            System.out.println("Enter the item to Push");

            int y=sc1.nextInt();          

            a[top]=y;

            top++;

        }

    }

  

    void pop()

    {

        if(top <= 0)

        {

            System.out.println("Stack empty");

        }

        else

        {    top-- ;

            System.out.println("Popped element is:"+a[top]) ;

          

        }

  }

}

  class Mainstack

{

    public static void main(String args[])

    {

     try

     {

        Stack s= new Stack();

        Scanner sc=new Scanner(System.in);

        int a;

        do

        {

        System.out.println("Choose any one <1,2> to do follwing operation \n 1) PUSH \n 2) POP ");

        int x=sc.nextInt();

        switch(x)

        {

            case 1:

                s.push();

                break;

            case 2:

                s.pop();

                break;

            default :

                System.out.println("Thank You!!!");

                break;

        }

        System.out.print("Do you wish to continue \n Press \n 1 to continue \n 0 to exit :");

        a=sc.nextInt();

        }  

        while(a==1);

     }

     catch(Exception e)

     {

        System.out.println(e);

     }  

    }

}


 

Output:

Choose any one <1,2> to do follwing operation

1) PUSH

2) POP

1

Enter the item to Push

10

Do you wish to continue

Press

1 to continue

0 to exit :1

Choose any one <1,2> to do follwing operation

1) PUSH

2) POP

1

Enter the item to Push

12

Do you wish to continue

Press

1 to continue

0 to exit :1

Choose any one <1,2> to do follwing operation

1) PUSH

2) POP

1

Enter the item to Push

11

Do you wish to continue

Press

1 to continue

0 to exit :1

Choose any one <1,2> to do follwing operation

1) PUSH

2) POP

1

Enter the item to Push

15

Do you wish to continue

Press

1 to continue

0 to exit :1

Choose any one <1,2> to do follwing operation

1) PUSH

2) POP

1

Stack full

Do you wish to continue

Press

1 to continue

0 to exit :1

Choose any one <1,2> to do follwing operation

1) PUSH

2) POP

2

Popped element is:15

Do you wish to continue

Press

1 to continue

0 to exit :1

Choose any one <1,2> to do follwing operation

1) PUSH

2) POP

2

Popped element is:11

Do you wish to continue

Press

1 to continue

0 to exit :1

Choose any one <1,2> to do follwing operation

1) PUSH

2) POP

2

Popped element is:12

Do you wish to continue

Press

1 to continue

0 to exit :1

Choose any one <1,2> to do follwing operation

1) PUSH

2) POP

2

Popped element is:10

Do you wish to continue

Press

1 to continue

0 to exit :1

Choose any one <1,2> to do follwing operation

1) PUSH

2) POP

2

Stack empty

Do you wish to continue

Press

1 to continue

0 to exit :0

No comments:

Post a Comment

Followers