-
Notifications
You must be signed in to change notification settings - Fork 0
/
01670. Design Front Middle Back Queue.rs
66 lines (56 loc) · 1.57 KB
/
01670. Design Front Middle Back 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
struct FrontMiddleBackQueue {
vec: Vec<i32>
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl FrontMiddleBackQueue {
fn new() -> Self {
return FrontMiddleBackQueue {
vec: Vec::new()
}
}
fn push_front(&mut self, val: i32) {
self.vec.insert(0, val);
}
fn push_middle(&mut self, val: i32) {
let middle_position = self.vec.len() / 2;
self.vec.insert(middle_position, val);
}
fn push_back(&mut self, val: i32) {
self.vec.push(val);
}
fn pop_front(&mut self) -> i32 {
if self.vec.len() > 0 {
let return_val = self.vec[0];
self.vec.remove(0);
return return_val;
} else {
return -1;
}
}
fn pop_middle(&mut self) -> i32 {
if self.vec.len() > 0 {
let middle_position = (self.vec.len() + 1) / 2;
let return_val = self.vec[middle_position - 1];
self.vec.remove(middle_position - 1);
return return_val;
} else {
return -1;
}
}
fn pop_back(&mut self) -> i32 {
return self.vec.pop().unwrap_or(-1);
}
}
/**
* Your FrontMiddleBackQueue object will be instantiated and called as such:
* let obj = FrontMiddleBackQueue::new();
* obj.push_front(val);
* obj.push_middle(val);
* obj.push_back(val);
* let ret_4: i32 = obj.pop_front();
* let ret_5: i32 = obj.pop_middle();
* let ret_6: i32 = obj.pop_back();
*/