import java.io.*;
public class StackImplementation
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int max;
int top;
int stack[];
public StackImplementation()
{
top=0;
max=0;
stack=new int[max];
}
public void Push(int num)
{
System.out.println("Top: "+top+"and max: "+max);
if(top==max)
System.out.println("STACK OVERFLOW!!!");
else
{
stack[top]=num;
top++;
System.out.println(num+" is pushed in the stack!");
}
}
public void Pop()
{
if(top==0)
System.out.println("STACK UNDERFLOW!!!");
else
{
stack[top]=0;
top--;
System.out.println("Element poped!!!");
}
}
public void display()
{
if(top==0)
System.out.println("STACK UNDERFLOW!!NO ELEMENTS TO SHOW!!");
else
{
for(int i=top;i>0;i--)
{
if(stack[i]!=0)
System.out.print(stack[i]+" ");
else
System.out.print("");
}
}
}
public void main(String args[]) throws IOException
{
StackImplementation obj= new StackImplementation();
System.out.println("----STACK AS AN ARRAY IMPLEMENTATION----");
max=Integer.parseInt(br.readLine());
System.out.println("MAX:"+ max);
int inpt=0,n;
do
{
System.out.println("1)Enter 1 for performing Push\n2)Enter 2 for performing Pop\n3)Enter 3 for Displaying\n"
+"4)Enter 4 for Exiting");
inpt=Integer.parseInt(br.readLine());
switch(inpt)
{
case 1:
System.out.println("Enter number to be Pushed :");
n=Integer.parseInt(br.readLine());
obj.Push(n);
break;
case 2:
obj.Pop();
break;
case 3:
obj.display();
case 4:
System.out.println("******Program Terminates******");
System.exit(0);
default:
System.out.println("Wrong Entry!!");
}
}while(inpt!=4);
}
}