

1. SPECIAL_QUEUE() is a function that initializes your queue.
2. PUSH_FRONT(VAL) inserts the VAL in the front of the queue.
3. PUSH_BACK(VAL) inserts the VAL at the back of the queue.
4. PUSH_MIDDLE(VAL) inserts the VAL at the middle position of the queue.
5. POP_FRONT(VAL) removes the first element from the queue and returns the value of the removed element.
6. POP_BACK(VAL) removes the last element from the queue and returns the value of the removed element.
7. POP_MIDDLE(VAL) removes the middle element from the queue and returns the value of the removed element.
If the queue is empty and any type of ‘POP’ function is called, return -1.
If there are two middle positions available, operate on the position close to the front of the queue.
If the functions calls are as follows:
‘PUSH_FRONT’(10).
‘PUSH_FRONT’(20)’.
‘PUSH_MIDDLE’(30)
‘POP_BACK’().
So after each operation queue will changes like [ ] -> [10] -> [20, 10] -> [20, 30, 10] ->[20,30].
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer, 'N’, denoting the number of function calls.
The second line contains a list of ‘N’ function names.
The third line of each test case contains ‘N’ integers denoting the parameters passed in each function call. (Parameter value corresponding to any pop() function is -1.)
For each test case, output the value of the popped element for each pop() function call in a separate line.
For push() function ,do not print anything.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10000.
0 <= ‘VAL’ <= 10^6 .
Time limit: 1 sec
In this approach, we will implement this special queue using a simple dynamic array.
In push functions, we will shift the elements of the array and insert the new element.
In pop functions, we will remove the element from the specified position and update the array.
In these functions, we will use additional space to change the array according to the function.
In this approach, we will implement this special queue using two deque.
Deque (Doubly Ended Queue) is an inbuilt queue in each insertion and deletion at both end can be done in O(1) time.
We will maintain the condition that the size of ‘FIRST’ deque is always equal to the size of ‘SECOND’ deque or( size of ‘SECOND’ deque -1 ). So in the ‘PUSH_MIDDLE’ function,we will always push the element in the ‘FIRST’ deque. We will also define two functions FIRST_TO_SECOND() to transfer the last element of ‘FIRST’ into ‘SECOND’, and SECOND_TO_FIRST() to transfer the first element of ‘SECOND’ into ‘FIRST’.
5In this approach, we will implement this special queue using a doubly-linked list.
We will define the Node structure which has one integer variable ‘VALUE’ to store the value and two pointers ‘NEXT’ and ‘PREV’ to point to the next and previous node.
We will declare three-pointers ‘HEAD’,’ TAIL’ and ‘MID’ to point to the respective positions.
We will also declare a variable ‘QUEUE_SIZE’ to store the number of elements currently have in the queue.
In the functions, we will add or remove the respective node from the list and update the list accordingly.