Problem 893
Design Front Middle Back Queue
Medium · 20 pts · Design, Queue, Linked List
Problem
Simulate a queue that supports pushes and pops at the front, the middle and the back. 'pushFront v', 'pushMiddle v' and 'pushBack v' insert v; the middle position is index floor(size / 2), so for an even size the new value goes just after the first half. 'popFront', 'popMiddle' and 'popBack' remove and print a value, or print -1 if the queue is empty; popMiddle removes index floor((size - 1) / 2), the front-most middle element.
Input
The first line contains Q. Each of the next Q lines contains one operation. There is at least one pop.
Output
Print the result of every pop, one per line.
Constraints
1 <= Q <= 1000; 1 <= v <= 10^9
Example input
9 pushFront 1 pushBack 2 pushMiddle 3 pushMiddle 4 popFront popMiddle popMiddle popBack popFront
Example output
1 3 4 2 -1
Explanation
pushFront 1, pushBack 2 and pushMiddle 3 build 1 3 2, then pushMiddle 4 builds 1 4 3 2. popFront prints 1, popMiddle removes the front-most middle 3, popMiddle prints 4, popBack prints 2, and popFront on the empty queue prints -1.