-
Notifications
You must be signed in to change notification settings - Fork 0
/
implement-queue-using-stacks.rs
65 lines (55 loc) · 1.16 KB
/
implement-queue-using-stacks.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
#![allow(unused)]
struct MyQueue {
inner: Vec<i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MyQueue {
fn new() -> Self {
MyQueue { inner: Vec::new() }
}
fn push(&mut self, x: i32) {
self.inner.push(x);
}
fn pop(&mut self) -> i32 {
self.inner.remove(0)
}
fn peek(&self) -> i32 {
if let Some(x) = self.inner.first() {
*x
} else {
0
}
}
fn empty(&self) -> bool {
self.inner.is_empty()
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* let obj = MyQueue::new();
* obj.push(x);
* let ret_2: i32 = obj.pop();
* let ret_3: i32 = obj.peek();
* let ret_4: bool = obj.empty();
*/
fn main() {
let mut obj = MyQueue::new();
obj.push(1);
obj.push(2);
}
#[cfg(test)]
mod tests {
use crate::MyQueue;
#[test]
fn test() {
let mut obj = MyQueue::new();
obj.push(1);
obj.push(2);
assert_eq!(obj.peek(), 1);
assert_eq!(obj.pop(), 1);
assert!(!obj.empty());
}
}