import java.io.*;
class Aray
{
private int a[];
public int t=-1,i;
public Aray()
{
a=new int[5];
}
public void push(int val)
{
if(t<4)
{
t++;
a[t]=val;
}
else
System.out.println("Stack is full");
}
public int pop()
{
if(t==-1)
{
System.out.println("Stack is Empty");
return 0;
}
else
{
int temp = a[t];
t--;
return temp;
}
}
public void display()
{
if(t==-1)
{
System.out.println("Stack is Empty");
}
else
{
System.out.println("Elements from top to bottom");
for(i=t;i>-1;i--)
System.out.println(a[i]);
}
}
}
class StackArray
{
public static void main(String[] args) throws IOException
{
Aray a = new Aray();
System.out.println("1.PUSH\n2.POP\n3.DISPLAY");
while(true)
{
System.out.println("Enter the Key of the Operation");
int c=Integer.parseInt((new BufferedReader(new InputStreamReader(System.in))).readLine());
switch(c)
{
case 1:
System.out.println("Enter the Element");
int val=Integer.parseInt((new BufferedReader(new InputStreamReader(System.in))).readLine());
a.push(val);
break;
case 2:
int temp=a.pop();
if(temp!=0)
System.out.println("Element popped is [" + temp + "] ");
break;
case 3:
a.display();
break;
case 4:
System.exit(0);
default:
System.out.println("You have entered invalid Key.\n Try again");
}
}
}
}
No comments:
Post a Comment