1670. Design Front Middle Back Queue Medium

@problem@discussion
#Array#Linked List#Design#Queue#Data Stream



1/**
2 * [1670] Design Front Middle Back Queue
3 *
4 * Design a queue that supports push and pop operations in the front, middle, and back.
5 * Implement the FrontMiddleBack class:
6 * 
7 * 	FrontMiddleBack() Initializes the queue.
8 * 	void pushFront(int val) Adds val to the front of the queue.
9 * 	void pushMiddle(int val) Adds val to the middle of the queue.
10 * 	void pushBack(int val) Adds val to the back of the queue.
11 * 	int popFront() Removes the front element of the queue and returns it. If the queue is empty, return -1.
12 * 	int popMiddle() Removes the middle element of the queue and returns it. If the queue is empty, return -1.
13 * 	int popBack() Removes the back element of the queue and returns it. If the queue is empty, return -1.
14 * 
15 * Notice that when there are two middle position choices, the operation is performed on the frontmost middle position choice. For example:
16 * 
17 * 	Pushing 6 into the middle of [1, 2, 3, 4, 5] results in [1, 2, <u>6</u>, 3, 4, 5].
18 * 	Popping the middle from [1, 2, <u>3</u>, 4, 5, 6] returns 3 and results in [1, 2, 4, 5, 6].
19 * 
20 *  
21 * Example 1:
22 * 
23 * Input:
24 * ["FrontMiddleBackQueue", "pushFront", "pushBack", "pushMiddle", "pushMiddle", "popFront", "popMiddle", "popMiddle", "popBack", "popFront"]
25 * [[], [1], [2], [3], [4], [], [], [], [], []]
26 * Output:
27 * [null, null, null, null, null, 1, 3, 4, 2, -1]
28 * Explanation:
29 * FrontMiddleBackQueue q = new FrontMiddleBackQueue();
30 * q.pushFront(1);   // [<u>1</u>]
31 * q.pushBack(2);    // [1, <u>2</u>]
32 * q.pushMiddle(3);  // [1, <u>3</u>, 2]
33 * q.pushMiddle(4);  // [1, <u>4</u>, 3, 2]
34 * q.popFront();     // return 1 -> [4, 3, 2]
35 * q.popMiddle();    // return 3 -> [4, 2]
36 * q.popMiddle();    // return 4 -> [2]
37 * q.popBack();      // return 2 -> []
38 * q.popFront();     // return -1 -> [] (The queue is empty)
39 * 
40 *  
41 * Constraints:
42 * 
43 * 	1 <= val <= 10^9
44 * 	At most 1000 calls will be made to pushFront, pushMiddle, pushBack, popFront, popMiddle, and popBack.
45 * 
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/design-front-middle-back-queue/
50// discuss: https://leetcode.com/problems/design-front-middle-back-queue/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54struct FrontMiddleBackQueue {
55        false
56    }
57
58
59/** 
60 * `&self` means the method takes an immutable reference.
61 * If you need a mutable reference, change it to `&mut self` instead.
62 */
63impl FrontMiddleBackQueue {
64
65    fn new() -> Self {
66        
67    }
68    
69    fn push_front(&self, val: i32) {
70        
71    }
72    
73    fn push_middle(&self, val: i32) {
74        
75    }
76    
77    fn push_back(&self, val: i32) {
78        
79    }
80    
81    fn pop_front(&self) -> i32 {
82        
83    }
84    
85    fn pop_middle(&self) -> i32 {
86        
87    }
88    
89    fn pop_back(&self) -> i32 {
90        
91    }
92}
93
94/**
95 * Your FrontMiddleBackQueue object will be instantiated and called as such:
96 * let obj = FrontMiddleBackQueue::new();
97 * obj.push_front(val);
98 * obj.push_middle(val);
99 * obj.push_back(val);
100 * let ret_4: i32 = obj.pop_front();
101 * let ret_5: i32 = obj.pop_middle();
102 * let ret_6: i32 = obj.pop_back();
103 */
104
105// submission codes end
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    #[test]
112    fn test_1670() {
113    }
114}
115


Back
© 2025 bowen.ge All Rights Reserved.