Tuesday, 15 November 2011

stack

import java.util.*;
import java.io.*;
class StackD {
static void showpush(Stack st, int a) {
st.push(new Integer(a));
System.out.println("push(" + a + ")");
System.out.println("stack: " + st);
}
static void showpop(Stack st) {
System.out.print("pop -> ");
Integer a = (Integer) st.pop();
System.out.println(a);
System.out.println("stack: " + st);
}
public static void main(String args[])throws IOException
 {
Stack st = new Stack();
DataInputStream d=new DataInputStream(System.in);
int will=1;
System.out.println("stack: " + st);
while(will==1)
{
System.out.println("Enter your choice:");
System.out.println("*****1.Push      2.Pop   3.Exit*****");

int c=Integer.parseInt(d.readLine());
switch(c)
{
case 1:
{
System.out.println("Enter a number to stack:");
int n=Integer.parseInt(d.readLine());
showpush(st, n);
}
break;
case 2:
{
try {
showpop(st);
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
break;
default:
System.out.println("Invalid Choice");
}
System.out.println("Do U Want To Continue? 1.Yes 2.No");
will=Integer.parseInt(d.readLine());
}
}
}

No comments:

Post a Comment