From 67a84f74c134a519c4681e00f89fe9f0663c66d3 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 29 Oct 2022 18:17:20 +0800 Subject: [PATCH 1/2] =?UTF-8?q?update=200232.=E7=94=A8=E6=A0=88=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=98=9F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0232.用栈实现队列.md | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 1ed0c5d6..e6a58be3 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -622,6 +622,48 @@ class MyQueue() { } ``` +rust: + +```rust +struct MyQueue { + stack_in: Vec, + stack_out: Vec, +} +impl MyQueue { + + fn new() -> Self { + MyQueue { + stack_in: Vec::new(), + stack_out: Vec::new(), + } + + } + + fn push(&mut self, x: i32) { + self.stack_in.push(x); + } + + fn pop(&mut self) -> i32 { + if self.stack_out.is_empty(){ + while !self.stack_in.is_empty() { + self.stack_out.push(self.stack_in.pop().unwrap()); + } + } + self.stack_out.pop().unwrap() + } + + fn peek(&mut self) -> i32 { + let res = self.pop(); + self.stack_out.push(res); + res + } + + fn empty(&self) -> bool { + self.stack_in.is_empty() && self.stack_out.is_empty() + } +} +``` +

From 7950be58a29b0757189d3afdb25ccf45e1bbf2a6 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 29 Oct 2022 18:18:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200232.=E7=94=A8=E6=A0=88=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=98=9F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0232.用栈实现队列.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index e6a58be3..5858f9e0 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -630,7 +630,6 @@ struct MyQueue { stack_out: Vec, } impl MyQueue { - fn new() -> Self { MyQueue { stack_in: Vec::new(),