import java.io.*;
public class stack
{
int arr[]=new int[10];
int f,r;
int i,n;
int check;
String str;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
stack()
{
f=-1;
r=-1;
}
public void status() throws Exception
{
System.out.println("\nEnter '1' for Input restricted Dequeue,
'2' for Output restricted Dequeue:");
check=Integer.parseInt(br.readLine());
}
public void push() throws IOException
{
if(r==9)
{
System.out.println("Queue
overflow...");
return;
}
if(check!=1)
{
System.out.println("Specify the location ( Front or
rear):");
str=br.readLine().toLowerCase();
}
else
str="r";
System.out.println("Enter the value to insert: ");
n=Integer.parseInt(br.readLine());
if(f==-1)
{
arr[++f]=n;
r=0;
}
else if(str.charAt(0)=='f')
{
for(i=r+1;i>f;i--)
arr[i]=arr[i-1];
arr[i]=n;
r++;
}
else
{
arr[++r]=n;
}
}
public void display()
{
if(f==-1)
return;
for(i=f;i<=r;i++)
System.out.print("
"+arr[i]);
}
public void pop() throws IOException
{
if(f==-1)
{
System.out.println("Queue
Underflow...");
return;
}
if(check!=2)
{
System.out.println("Specify the location ( Front or
rear):");
str=br.readLine().toLowerCase();
}
else
str="f";
if(f==r)
{
f=-1;
r=-1;
}
else if(str.charAt(0)=='f')
{
f++;
}
else
{
r--;
}
}
public static void main(String args[])throws Exception
{
char op;
BufferedReader br;
stack ob=new stack();
ob.status();
while(true)
{
br=new
BufferedReader(new InputStreamReader(System.in));
System.out.println("\nPress 'p' for push, 'd' for pop and 'q' for
quite:");
op=(char)br.read();
if(op=='p')
ob.push();
else if(op=='d')
ob.pop();
ob.display();
if(op=='q')
break;
br=null;
}
}
}