Wednesday, 9 October 2013

JAVA PROGRAMS 8

Write a Java program that:
i)     Implements stack ADT.

PROGRAM:

import java.util.*;

public class StackADT
{
        int a[];
        int top;
            public StackADT(int n)         
            {         
                        a = new int[n];
                        top = -1;         
            }
            public void push(int item)
            {
                if(isFull())
                        {
                                    System.out.println("Stack is full");
                        return;
                        }
                a[++top] = item;
                System.out.println("Item Pushed");
            }
            public int pop()
            {
                if(isEmpty()) 
                        {         
                                    System.out.println("Stack is empty");
                        return 0;
                        }  
                return a[top--];
            }
            public int topElement()   
            {         
                if(isEmpty())           return 0;
                else                          return a[top];
            }
            public boolean isEmpty()    
            {         
                return (top <= -1);
            }
        public boolean isFull()
        {
                return (top >= a.length-1);
        }
        public int count()
        { 
                        return top+1;
         }
        public void display()
        {
                 System.out.print("Items In Stack Are: ");
                 for(int i=0;i<=top;i++)
                        System.out.print(" "+a[i]);
        }
        public static void main(String args[])
        {
                int x,n,ch=0;
                Scanner sc=new Scanner(System.in);
                System.out.println("Enter Stack Size:");
                n=sc.nextInt();
                StackADT st=new StackADT(n);
                System.out.println("\t-----LIST OF OPERATIONS------");
                System.out.println("\t\t[1].Push");
                System.out.println("\t\t[2].Pop");
                System.out.println("\t\t[3].Top Element");
                System.out.println("\t\t[4].Count");
                System.out.println("\t\t[5].Empty");
                System.out.println("\t\t[6].Full");
                System.out.println("\t\t[7].Display");
                System.out.println("\t\t[0].EXIT");
                do
                {
                System.out.print("\nEnter Choice Of Operation:");
                ch=sc.nextInt();
                switch(ch)
                {
                        case 1: System.out.print("Enter Element: ");
                                x=sc.nextInt();
                                st.push(x);
                                break;
                        case 2: x=st.pop();
                                if(x!=0)                System.out.println("Item Poped: "+x);
                                break;
                        case 3: x=st.topElement();
                                if(x!=0)                System.out.println("Top Item: "+x);
                                else                      System.out.println("Stack Is Empty");
                                break;
                        case 4: System.out.println("Number Of Items In Stack: "+st.count());
                                break;
                        case 5: if(st.isEmpty()) System.out.println("Stack Is Empty");
                                else                        System.out.println("Stack Is Not Empty");
                                break;
                        case 6: if(st.isFull())     System.out.println("Stack Is Full");
                                else                        System.out.println("Stack Is Not Full");
                                break;
                        case 7: if(st.isEmpty()) System.out.println("Stack Is Empty");
                                else                        st.display();
                        case 0: break;
                        default: System.out.println("Invalid Choice Of Operation");
                        }
                }while(ch!=0);              }    }

Output:
   -----LIST OF OPERATIONS------
                [1].Push
                [2].Pop
                [3].Top Element
                [4].Count
                [5].Empty
                [6].Full
                [7].Display
                [0].EXIT

Enter Stack Size:
10
Enter Choice Of Operation:1
Enter Element: 10
Item Pushed
Enter Choice Of Operation:1
Enter Element: 20
Item Pushed
Enter Choice Of Operation:7
Items In Stack Are:  10 20
Enter Choice Of Operation:3
Top Item: 10
Enter Choice Of Operation:4
Number Of Items In Stack: 1

Enter Choice Of Operation:0

FREE HIT COUNTERS