-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
54 lines (45 loc) · 934 Bytes
/
lib.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
/*!
* # 232. Implement Queue using Stacks
*
* * [Problem link](https://leetcode.com/problems/implement-queue-using-stacks/)
*/
#![allow(dead_code)]
struct MyQueue {
start: usize,
data: Vec<i32>,
}
impl MyQueue {
fn new() -> Self {
MyQueue {
start: 0,
data: vec![],
}
}
fn push(&mut self, x: i32) {
self.data.push(x);
}
fn pop(&mut self) -> i32 {
let v = self.data[self.start];
self.start += 1;
v
}
fn peek(&self) -> i32 {
self.data[self.start]
}
fn empty(&self) -> bool {
self.start == self.data.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_example() {
let mut queue = MyQueue::new();
queue.push(1);
queue.push(2);
assert_eq!(queue.peek(), 1);
assert_eq!(queue.pop(), 1);
assert!(!queue.empty());
}
}