06-04-2011, 01:14 PM
Code:
import java.io.*;
import java.util.*;
class Demo
{
static final int max=4;
int a[]=new int[max];
int top;
Demo()
{
top=-1;
}
void push(int item)
{
if(top==max)
System.out.println("Stack is full");
else
a[++top]=item;
}
int pop()
{
if(top==-1)
return 0;
else
return a[top--];
}
void display()
{
System.out.println("New Stack Is:");
for(int i=0;i<=top;i++)
System.out.println(a[i]);
}
}
class Stacks
{
public static void main(String args[])throws IOException
{
DataInputStream dim=new DataInputStream(System.in);
Demo s=new Demo();
int op;
do
{
System.out.println("\n1.push\n2.pop\n3.display4.exit\n");
op=Integer.parseInt(dim.readLine());
switch(op)
{
case 1:System.out.println("enter item to push");
int n=Integer.parseInt(dim.readLine());
s.push(n);
break;
case 2:int f=s.pop();
if(f==0)
{
System.out.println("empty stack:");
}
else
{
System.out.println("poped element is"+f);
}
break;
case 3:s.display();break;
case 4:break;
default:System.out.println("wrong choice?");
}
}while(op!=4);
}
}
OUTPUT
E:\ 5BCA-B\lijo\java >java Stacks
1.push
2.pop
3.display
4.exit
1
enter item to push
1
1.push
2.pop
3.display
4.exit
1
enter item to push
2
1.push
2.pop
3.display
4.exit
3
New Stack Is:
1
2
1.push
2.pop
3.display
4.exit
2
poped element is2
1.push
2.pop
3.display
4.exit
3
New Stack Is:
1
1.push
2.pop
3.display 4.exit 4