Skip to content

Commit f496c53

Browse files
committed
finish algorithm8
1 parent fb11dce commit f496c53

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

exercises/algorithm/algorithm7.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ fn bracket_match(bracket: &str) -> bool
120120
if !matches!(
121121
(top, ch),
122122
('(', ')') | ('{', '}') | ('[', ']')
123-
) {
123+
)
124+
{
124125
return false;
125126
}
126127
}

exercises/algorithm/algorithm8.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,25 @@ impl<T> myStack<T> {
6767
}
6868
}
6969
pub fn push(&mut self, elem: T) {
70-
//TODO
70+
self.q2.enqueue(elem);
71+
// 2. 把 q1 中所有元素转移到 q2
72+
while !self.q1.is_empty() {
73+
let v = self.q1.dequeue().unwrap();
74+
self.q2.enqueue(v);
75+
}
76+
// 3. 交换 q1 和 q2
77+
std::mem::swap(&mut self.q1, &mut self.q2);
7178
}
7279
pub fn pop(&mut self) -> Result<T, &str> {
73-
//TODO
74-
Err("Stack is empty")
80+
if self.q1.is_empty() {
81+
Err("Stack is empty")
82+
} else {
83+
self.q1.dequeue()
84+
}
7585
}
7686
pub fn is_empty(&self) -> bool {
7787
//TODO
78-
true
88+
self.q1.is_empty()
7989
}
8090
}
8191

0 commit comments

Comments
 (0)