/* STACK USING ARRAY */
import java.io.*;
class Stack
{
int top,a[],n;
DataInputStream nn=new
DataInputStream(System.in);
void read()
{
try
{
System.out.print("enter the limit :
");
n=Integer.parseInt(nn.readLine());
a=new int[n];
top=0;;
}
catch(Exception e)
{}}
void push()
{
try
{
if(top==n)
System.out.print("stack is
full\n");
else
{
System.out.print("Enter the
number: ");
int d=Integer.parseInt(nn.readLine());
a[top]=d;
top++;
}}
catch(Exception e)
{}
}
void pop()
{
if(top==0)
System.out.println("stack is
empty");
else
{
top--;
int d=a[top];
System.out.println("poped element is:
" +d);
}}
void display()
{
if(top==0)
System.out.println("no
elements");
else
{
for(int i=0;i<top;i++)
System.out.println(a[i]);
}
}
}
class Mystack
{
public static void main(String
args[])throws Exception
{
try
{
DataInputStream get=new
DataInputStream(System.in);
int p;
Stack obj=new Stack();
obj.read();
do
{
System.out.print("1.push\n2.pop\n3.Display");
System.out.print("\nEnter the choice: ");
int ch=Integer.parseInt(get.readLine());
p=ch;
switch(ch)
{
case 1: obj.push();break;
case 2: obj.pop();break;
case 3: obj.display();
}
}while(p<4);
}
catch(Exception e)
{}
}
}
OUTPUT
enter the limit : 2
1.push
2.pop
3.Display
Enter the choice: 1
Enter the number: 10
1.push
2.pop
3.Display
Enter the choice: 1
Enter the number: 20
1.push
2.pop
3.Display
Enter the choice: 1
stack is full
1.push
2.pop
3.Display
Enter the choice: 2
poped element is: 20
1.push
2.pop
3.Display
Enter the choice: 2
poped element is: 10
1.push
2.pop
3.Display
Enter the choice: 2
stack is empty
1.push
2.pop
3.Display
Enter the choice: 3
no elements
1.push
2.pop
3.Display
Enter the choice: 4






0 comments:
Post a Comment