-
Notifications
You must be signed in to change notification settings - Fork 3
/
02-design-circular-queue.rs
73 lines (65 loc) · 1.59 KB
/
02-design-circular-queue.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
struct MyCircularQueue {
front: usize,
end: usize,
arr: Vec<i32>,
size: usize,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MyCircularQueue {
fn new(k: i32) -> Self {
Self {
arr: vec![0; k as usize],
front: 0,
end: 0,
size: 0,
}
}
fn en_queue(&mut self, value: i32) -> bool {
if self.is_full() {
return false;
}
self.size += 1;
self.arr[self.end] = value;
self.end = (self.end + 1) % self.arr.len();
true
}
fn de_queue(&mut self) -> bool {
if self.is_empty() {
return false;
}
self.size -= 1;
self.front = (self.front + 1) % self.arr.len();
true
}
fn front(&self) -> i32 {
if self.is_empty() {
return -1;
}
self.arr[self.front]
}
fn rear(&self) -> i32 {
if self.is_empty() {
return -1;
}
self.arr[(self.end + self.arr.len() - 1) % self.arr.len()]
}
fn is_empty(&self) -> bool {
self.size == 0
}
fn is_full(&self) -> bool {
self.size == self.arr.len()
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* let obj = MyCircularQueue::new(k);
* let ret_1: bool = obj.en_queue(value);
* let ret_2: bool = obj.de_queue();
* let ret_3: i32 = obj.front();
* let ret_4: i32 = obj.rear();
* let ret_5: bool = obj.is_empty();
* let ret_6: bool = obj.is_full();
*/