eolymp
bolt
Try our new interface for solving problems
Problems

Simple deque

Simple deque

Time limit 2 seconds
Memory limit 128 MiB

Implement the "deque" data structure. Write a program that describes the deque and simulates its operations. You must implement all methods given below. The program reads the sequence of commands and executes the corresponding operation. After executing each command the program must print one line. The possible commands for the program are:

push_front

Add (put) into the front of the deque the new element. The program must print ok.

push_back

Add (put) into the end of the deque the new element. The program must print ok.

pop_front

Pop the first element from the deque. The program must print its value.

pop_back

Pop the last element from the deque. The program must print its value.

front

Find the value of the first element (not deleting it). The program must print its value.

back

Find the value of the last element (not deleting it). The program must print its value

size

Print the number of elements in the deque.

clear

Clear the deque (delete all its elements) and print ok.

exit

The program must print bye and terminate.

It is guaranteed that the number of elements in the deque at any time does not exceed 100. All the operations:

  • pop_front,

  • pop_back,

  • front,

  • back

are usually correct.

// Java OOP
class MyDeque
{
  protected LinkedList<Integer> v;
  MyDeque(); 
  public void push_back(int x);
  public void push_front(int x);
  public int pop_back();
  public int pop_front();
  public int front();
  public int back();
  public int size();
  public void clear();
  public void exit();
}

Input data

Each line contains one command.

Output data

For each command, print the corresponding result on a separate line.

Examples

Input example #1
push_back 3
push_front 14
size
clear
push_front 1
back
push_back 2
front
pop_back
size
pop_front
size
exit
Output example #1
ok
ok
2
ok
ok
1
ok
1
2
1
1
0
bye