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
struct CQueue {
s1: Vec<i32>,
s2: Vec<i32>,
}
#[allow(unused)]
impl CQueue {
fn new() -> Self {
CQueue { s1: Vec::new(), s2: Vec::new() }
}
fn append_tail(&mut self, value: i32) {
self.s1.push(value);
}
fn delete_head(&mut self) -> i32 {
if self.s2.is_empty() {
while !self.s1.is_empty() {
self.s2.push(self.s1.pop().unwrap());
}
}
self.s2.pop().unwrap_or(-1)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let mut obj = CQueue::new();
obj.append_tail(32);
assert_eq!(32, obj.delete_head());
assert_eq!(-1, obj.delete_head());
obj.append_tail(3);
assert_eq!(3, obj.delete_head());
assert_eq!(-1, obj.delete_head());
obj.append_tail(5);
obj.append_tail(2);
assert_eq!(5, obj.delete_head());
assert_eq!(2, obj.delete_head());
}
}