eolymp
bolt
Try our new interface for solving problems
Problems

Deque with error protection

Deque with error protection

Implement the "deque" data structure. Write a program that contains the description of the deck and implement all the methods mentioned below. The program reads the sequence of commands and, depending on the command performs an operation. After each command, the program should print one line. Possible commands for the program are:

push_front

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

push_back

Add (put) the element in back of the deque. The program must print ok.

pop_front

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

pop_back

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

front

Give the value of the first element (not removing it). The program must print its value.

back

Give the value of the last element (not removing it). The program must print its value.

size

Print the number of elements in the deque.

clear

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

exit

The program must print bye and finish.

It is guaranteed that the number of elements in the deque during runtime is no more than 100. Before processing the operations pop_front, pop_back, front, back the program must check if the deque contains at least one element. If you run the operation pop_front, popback, front, back and the deque is empty, the program should print line error instead of integer value.

// Java OOP
class MyDeque // from problem 6128 Simple deque
{
  protected LinkedList<Integer> v;
  ...
}

class ErrorProtectedDeque extends MyDeque
{
  final static int ERROR = -2000000000;
  public int pop_front();  // in the cases of error, return ERROR
  public int pop_back();  // in the cases of error, return ERROR
  public int front(); // in the cases of error, return ERROR
  public int back();  // in the cases of error, return ERROR
}

Input

Given in the problem statement. See sample input.

Output

Given in the problem statement. See sample output.

Time limit 2 seconds
Memory limit 128 MiB
Input example #1
front
back
pop_back
pop_front
exit

Output example #1
error
error
error
error
bye
Input example #2
push_back 1
back
exit
Output example #2
ok
1
bye

Example description: The number of elements in all data structures is no more than 10000.