import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Deque
{
static final int maxSize = 10000;
int size;
int head, end;
int[] data;
public Deque()
{
data = new int[maxSize];
head = 0;
end = 0;
size = 0;
}
public void push_front(int e)
{
if (size == maxSize)
{
System.
out.
println("Deque is full."); return;
}
else if (size==0)
{
end = head;
data[head] = e;
size++;
}
else
{
head++;
if (head>=maxSize)
head = 0;
data[head] = e;
size++;
}
}
public void push_back(int e)
{
if (size == maxSize)
{
System.
out.
println("Deque is full."); return;
}
else if (size==0)
{
head = end;
data[end] = e;
size++;
}
else
{
end--;
if (end<0)
end = maxSize-1;
data[end] = e;
size++;
}
}
public void clear()
{
head = 0;
end = 0;
size = 0;
}
public int size()
{
return size;
}
public int back()
{
if (size!=0)
return data[end];
else
return -1;
}
public int front()
{
if (size!=0)
return data[head];
else
return -1;
}
public int pop_back()
{
if (size!=0)
{
int tmp = data[end];
end++;
if (end>=maxSize)
end = 0;
size --;
return tmp;
}
else
return -1;
}
public int pop_front()
{
if (size!=0)
{
int tmp = data[head];
head--;
if (head<0)
head = maxSize - 1;
size --;
return tmp;
}
else
return -1;
}
}
class task
{
{
Deque dq = new Deque();
while((str = br.readLine()) != null)
{
String[] inp
= str.
split(" "); if (inp[0].equals("push_front"))
{
dq.
push_front(Integer.
parseInt(inp
[1])); }
else if (inp[0].equals("push_back"))
{
dq.
push_back(Integer.
parseInt(inp
[1])); }
else if (inp[0].equals("pop_back"))
{
System.
out.
println(dq.
pop_back()); }
else if (inp[0].equals("pop_front"))
{
System.
out.
println(dq.
pop_front()); }
else if (inp[0].equals("front"))
{
System.
out.
println(dq.
front()); }
else if (inp[0].equals("back"))
{
System.
out.
println(dq.
back()); }
else if (inp[0].equals("size"))
{
System.
out.
println(dq.
size()); }
else if (inp[0].equals("clear"))
{
dq.clear();
}
else if (inp[0].equals("exit"))
{
br.close();
return;
}
}
}
}
aW1wb3J0IGphdmEuaW8uQnVmZmVyZWRSZWFkZXI7CmltcG9ydCBqY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