622. Design Circular Queue Medium
1/**
2 * [622] Design Circular Queue
3 *
4 * Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
5 * One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
6 * Implementation the MyCircularQueue class:
7 *
8 * MyCircularQueue(k) Initializes the object with the size of the queue to be k.
9 * int Front() Gets the front item from the queue. If the queue is empty, return -1.
10 * int Rear() Gets the last item from the queue. If the queue is empty, return -1.
11 * boolean enQueue(int value) Inserts an element into the circular queue. Return true if the operation is successful.
12 * boolean deQueue() Deletes an element from the circular queue. Return true if the operation is successful.
13 * boolean isEmpty() Checks whether the circular queue is empty or not.
14 * boolean isFull() Checks whether the circular queue is full or not.
15 *
16 * You must solve the problem without using the built-in queue data structure in your programming language.
17 *
18 * Example 1:
19 *
20 * Input
21 * ["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"]
22 * [[3], [1], [2], [3], [4], [], [], [], [4], []]
23 * Output
24 * [null, true, true, true, false, 3, true, true, true, 4]
25 * Explanation
26 * MyCircularQueue myCircularQueue = new MyCircularQueue(3);
27 * myCircularQueue.enQueue(1); // return True
28 * myCircularQueue.enQueue(2); // return True
29 * myCircularQueue.enQueue(3); // return True
30 * myCircularQueue.enQueue(4); // return False
31 * myCircularQueue.Rear(); // return 3
32 * myCircularQueue.isFull(); // return True
33 * myCircularQueue.deQueue(); // return True
34 * myCircularQueue.enQueue(4); // return True
35 * myCircularQueue.Rear(); // return 4
36 *
37 *
38 * Constraints:
39 *
40 * 1 <= k <= 1000
41 * 0 <= value <= 1000
42 * At most 3000 calls will be made to enQueue, deQueue, Front, Rear, isEmpty, and isFull.
43 *
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/design-circular-queue/
48// discuss: https://leetcode.com/problems/design-circular-queue/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52struct MyCircularQueue {
53 false
54 }
55
56
57/**
58 * `&self` means the method takes an immutable reference.
59 * If you need a mutable reference, change it to `&mut self` instead.
60 */
61impl MyCircularQueue {
62
63 fn new(k: i32) -> Self {
64
65 }
66
67 fn en_queue(&self, value: i32) -> bool {
68
69 }
70
71 fn de_queue(&self) -> bool {
72
73 }
74
75 fn front(&self) -> i32 {
76
77 }
78
79 fn rear(&self) -> i32 {
80
81 }
82
83 fn is_empty(&self) -> bool {
84
85 }
86
87 fn is_full(&self) -> bool {
88
89 }
90}
91
92/**
93 * Your MyCircularQueue object will be instantiated and called as such:
94 * let obj = MyCircularQueue::new(k);
95 * let ret_1: bool = obj.en_queue(value);
96 * let ret_2: bool = obj.de_queue();
97 * let ret_3: i32 = obj.front();
98 * let ret_4: i32 = obj.rear();
99 * let ret_5: bool = obj.is_empty();
100 * let ret_6: bool = obj.is_full();
101 */
102
103// submission codes end
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn test_622() {
111 }
112}
113
Back
© 2025 bowen.ge All Rights Reserved.