Problem
Given a deque (Double Ended Queue) with the following query operations provided as the parameter queries, write a solution function that returns the results of executing these queries.
Query Information
"addFirst x"(xis a natural number)- Inserts the natural number
xat the front of the deque. - There is no change in the query result.
- Inserts the natural number
"addLast x"(xis a natural number)- Inserts the natural number
xat the end of the deque. - There is no change in the query result.
- Inserts the natural number
"removeFirst"- Adds the number at the front of the deque to the query result and removes that number from the deque.
- If the deque is empty, adds
0to the query result.
"removeLast"- Adds the number at the end of the deque to the query result and removes that number from the deque.
- If the deque is empty, adds
0to the query result.
Concept Explanation
A deque (Double Ended Queue) is a data structure that allows insertion and deletion from both ends, enabling it to function as either a stack or a queue depending on usage. Typically, the following four queries are applied to a deque:
addFirst/offerFirst X (Insert): Inserts object X at the front of the deque.addLast/offerLast X (Insert): Inserts object X at the end of the deque.removeFirst/pollFirst (Delete): Extracts and removes the object at the front of the deque.removeLast/pollLast (Delete): Extracts and removes the object at the end of the deque.
Additionally, various other queries can be applied:
getFirst/peekFirst: Views the object at the front of the deque without removing it. (Unlike pop, it does not remove the object)getLast/peekLast: Views the object at the end of the deque without removing it.empty: Returns whether the deque is currently empty.
Assume that the following queries are applied to the deque in order:
(addLast 4), (addFirst 10), (addLast 7), (removeFirst), (removeLast), (addFirst 5), (removeLast), (addFirst 15), (removeFirst)
The deque operation process is as follows.

Generally, a deque is implemented as a doubly linked list.
- The
addFirstandaddLastqueries operate at the front and back of the deque, respectively. - The
removeFirstandremoveLastqueries remove objects from the front and back of the deque, respectively.
A deque implemented as a doubly linked list has a time complexity of for both insertion and deletion queries.
Constraints
- All elements in
queriesare either"addFirst x"or"addLast x"wherexis a natural number between 0 and 10,000 inclusive, or"removeFirst","removeLast". - The length of
queriesis at most 100,000. queriesalways contain at least one"removeFirst"or"removeLast".
Test Case
| Test Case Number | queries | return |
|---|---|---|
| 1 | ["addLast 4","addFirst 10","addLast 7","removeFirst","removeLast","addFirst 5","removeLast","addFirst 15","removeFirst"] | [10,7,4,15] |
| 2 | ["addLast 5","addLast 5","addLast 5","removeFirst","addFirst 3","removeLast","removeFirst"] | [5,5,3] |
| 3 | ["removeFirst","addFirst 1","removeFirst","removeLast"] | [0,1,0] |
Explanation for Test Case 1
- This is identical to the example provided above.
Explanation for Test Case 2
-
The queries operate in the following manner.
Query Deque Result addLast 5 [5] [] addLast 5 [5,5] [] addLast 5 [5,5,5] [] removeFirst [5,5] [5] addFirst 3 [3,5,5] [5] removeLast [3,5] [5,5] removeFirst [5] [5,5,3]
Explanation for Test Case 3
-
The queries operate in the following manner. If the deque is empty, a pop query returns
0as the result.Query Deque Result removeFirst [] [0] addFirst 1 [1] [0] removeFirst [] [0,1] removeLast [] [0,1,0]