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, pop\_back, 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 data
Given in the problem statement. See sample input.
Output data
Given in the problem statement. See sample output.
Examples
front back pop_back pop_front exit
error error error error bye
push_back 1 back exit
ok 1 bye