-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexercise_10_1_6.rs
More file actions
58 lines (48 loc) · 1.11 KB
/
exercise_10_1_6.rs
File metadata and controls
58 lines (48 loc) · 1.11 KB
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
use super::super::extra::{Queue, Stack};
use super::super::ArrayStack;
pub struct ArrayStackQueue<T> {
front: ArrayStack<T>,
back: ArrayStack<T>,
}
impl<T> Default for ArrayStackQueue<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> ArrayStackQueue<T> {
#[must_use]
pub fn new() -> Self {
Self {
front: ArrayStack::new(),
back: ArrayStack::new(),
}
}
}
impl<T> Queue<T> for ArrayStackQueue<T> {
fn enqueue(&mut self, x: T) {
self.back.push(x);
}
fn dequeue(&mut self) -> T {
if self.front.empty() {
while !self.back.empty() {
self.front.push(self.back.pop());
}
}
self.front.pop()
}
fn empty(&self) -> bool {
self.front.empty() && self.back.empty()
}
fn length(&self) -> usize {
self.front.length() + self.back.length()
}
}
#[cfg(test)]
mod tests {
use super::super::super::tests;
use super::ArrayStackQueue;
#[test]
fn test_array_stack_queue() {
tests::run_queue_test_cases(ArrayStackQueue::new);
}
}