Simple deque#6128

Simple deque

2 seconds
128 megabytes
Very easy

Implement the deque data structure. Write a program that defines and simulates the operation of a deque, implementing all of the methods described below. The program will read a sequence of commands and execute the corresponding operations. After executing each command, the program must print a response on a separate line.

The possible commands are:

push_front

Add a new element to the front of the deque. The program must print ok.

push_back

Add a new element to the back of the deque. The program must print ok.

pop_front

Remove and output the first element from the deque.

pop_back

Remove and output the last element from the deque.

front

Output the value of the first element (without removing it).

back

Output the value of the last element (without removing it).

size

Print the number of elements in the deque.

clear

Clear the deque (remove all elements) and print ok.

exit

Print bye and terminate the program.

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

are always valid.

// 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

Each line contains a single command.

Output

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

Examples

Example 1

Loading
Loading