1670. Design Front Middle Back Queue
1670. Design Front Middle Back Queue
Description
Difficulty: Medium
Related Topics: Array, Linked List, Design, Queue, Data Stream
Design a queue that supports push and pop operations in the front, middle, and back.
Implement the FrontMiddleBack class:
FrontMiddleBack()Initializes the queue.void pushFront(int val)Addsvalto the front of the queue.void pushMiddle(int val)Addsvalto the middle of the queue.void pushBack(int val)Addsvalto the back of the queue.int popFront()Removes the front element of the queue and returns it. If the queue is empty, return-1.int popMiddle()Removes the middle element of the queue and returns it. If the queue is empty, return-1.int popBack()Removes the back element of the queue and returns it. If the queue is empty, return-1.
Notice that when there are two middle position choices, the operation is performed on the frontmost middle position choice. For example:
- Pushing
6into the middle of[1, 2, 3, 4, 5]results in[1, 2, <u>6</u>, 3, 4, 5]. - Popping the middle from
[1, 2, <u>3</u>, 4, 5, 6]returns3and results in[1, 2, 4, 5, 6].
Example 1:
1 | Input: |
Constraints:
- 1 <= val <= 109
- At most
1000calls will be made topushFront,pushMiddle,pushBack,popFront,popMiddle, andpopBack.
Hints/Notes
- use 2 queues
Solution
Language: C++
1 | class FrontMiddleBackQueue { |