Jun 3, 2017

Java program to push,pop,search,peek,display element

import java.io.*;
import java.util.*;
class lab5
{
 public static void main(String args[])throws IOException
{
 int ch;
 System.out.println("prog to object of a stack and use all methods ");
 Stack st=new Stack();
 InputStreamReader ins=new InputStreamReader(System.in);
 BufferedReader br=new BufferedReader(ins);
 String s=new String();
do
{
 System.out.println(".....more......");
 System.out.println("1.PUSH");
 System.out.println("2.POP");
 System.out.println("3.PEEK");
 System.out.println("4.DISPLAY");
 System.out.println("5.SEARCH");
 System.out.println("6.EXIT");
 System.out.println("ENTER UR CHOICE");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
 case 1: System.out.println("enter the ele to PUSH into stack;");
            s=br.readLine();
              st.push(new Integer(s));
                break;
 case 2:try
        {
            System.out.println("enter popped is"+st.pop());
        }
        catch(Exception e)
        {
                System.out.println("STACK is empty!");
        }
              break;
case 3:try{
            Integer a=(Integer)st.peek();
            System.out.println("Element on the top of the STACK is"+a);
           }
           catch(Exception e)
           {
               System.out.println("empty STACK!");
            }
            break;
case 4: System.out.println(st);
        break;
case 5: System.out.println("enter the ele to search:");
        int c=Integer.parseInt(br.readLine());
        int k=st.search(new Integer(c));
        if(k==-1)
             System.out.println("ele not found");
         else
          System.out.println("ele found at pos:"+k);
           break;
case 6: System.out.println("Exiting.");
          System.exit(0);
          break;
}
}
while(ch<6);
}}

No comments:

Post a Comment