Loading
View RSS Feed

guddu

How To Create Stack Class In Java?

Rating: 2 votes, 5.00 average.
Code:
import java.util.*;

class Operations{

      Scanner sc=new Scanner(System.in);

      int Maxsize,i,p,c=-1;

      int stk[];

      Operations(int m)

      {

            Maxsize=m;

            stk=new int[Maxsize];

      }

      void push()

      {

            if(c==(Maxsize-1))

                  System.out.println("\tStack Overflow");

            else

            {

                  System.out.println("Input an element : ");

                  i=sc.nextInt();

                  c++;  stk[c]=i;

            }

      }

      void pop()

      {

            if(c==-1)

                  System.out.println("\tStack is empty");

            else

            {

                  p=stk[c]; c--;

                  System.out.println("\t Element Poped : "+p);

            }

      }

      void display()

      {

            if(c==-1)

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

            else

            {

                  System.out.println("\nStack Status : ");

                  for(i=c;i>=0;i--)

                        System.out.println("\t "+stk[i]);

            }

      }

} 

class Stack{

      public static void main(String a[]){

            Scanner sc=new Scanner(System.in);

            int choice,size,i;

            System.out.println("Enter the size of stack : ");

            size=sc.nextInt(); 

            Operations s=new Operations(size); 
            for(;;)

            {

                  System.out.println("Enter your choice : ");

                  System.out.println(" 1. Push \n 2. Pop \n 3. Display Stack \n 4. exit  \t :::: ");

                  choice=sc.nextInt();

                  switch(choice)

                  {

                        case 1: s.push();

                                    break;

                        case 2: s.pop();

                                    break;

                        case 3: s.display();

                                    break;

                        default: System.exit(0);

                  }

            }

      }

}
output

Code:
Enter the size of stack :

4

Enter your choice :

1. Push

2. Pop

3. Display Stack

4. exit    ::::

1

Input an element :

1

Enter your choice :

1. Push

2. Pop

3. Display Stack

4. exit    ::::

1

Input an element :

19

Enter your choice :

1. Push

2. Pop

3. Display Stack

4. exit    ::::

3 

Stack Status :

       19

       1

Enter your choice :

1. Push

2. Pop

3. Display Stack

4. exit    ::::

2

       Element Poped : 19

Enter your choice :

1. Push

2. Pop

3. Display Stack

4. exit    ::::

2

       Element Poped : 1

Enter your choice :

1. Push

2. Pop

3. Display Stack

4. exit    ::::

2

      Stack is empty

Enter your choice :

1. Push

2. Pop

3. Display Stack

4. exit    ::::

3

      Stack is empty

Enter your choice :

1. Push

2. Pop

3. Display Stack

4. exit    ::::

Submit "How To Create Stack Class In Java?" to Digg Submit "How To Create Stack Class In Java?" to del.icio.us Submit "How To Create Stack Class In Java?" to StumbleUpon Submit "How To Create Stack Class In Java?" to Google

Updated 12-04-2011 at 06:53 PM by Harsh

Categories
Java , Data Structure

Comments




Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.