Sunday, September 26, 2010

Stack

import java.io.*;
class Stack
{
 Object ob[]=new Object[5];
 int top=-1;
 void push(Object x)
 {
   if(top==4)
   {
     System.out.println("Stack is full");
   }
   else
   {
     ob[++top]=x;
    }
 }
 void pop()
 {
  if(top==-1)
  {
    System.out.println("stack is empty");
   }
   else
    System.out.println("pop item:-"+ob[top--]);
  }
  void dis()
  {
   for(int i=top;i<=0;--i)
    System.out.println(ob[i]);
  }
  public static void main(String ar[])
  {
   DataInputStream p=new DataInputStream(System.in);
   Stack s=new Stack();
   int ch;
   Object x;
   try
   {
   while(true)
   {
   System.out.println("1.push \n 2.pop\n 3.dis\n 4.exit");
    System.out.println("Select:-");
    ch=System.in.read();
   switch(ch)
   {
     case 1:
             x=p.readLine();
             s.push(x);
             break;
     case 2:s.pop();
             break;
     case 3: s.dis();
               break;
//     case 4: exit();
    }
   }
      }
        catch(Exception e)
             {} 



  }

}

No comments:

Post a Comment