From 9769fefe0f4305eabbf275d2db61e8cdd2871c31 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 29 Oct 2022 18:47:41 +0800 Subject: [PATCH] =?UTF-8?q?update=200225.=E7=94=A8=E9=98=9F=E5=88=97?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=A0=88.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0225.用队列实现栈.md | 40 ++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index 9aa8d8a1..2f015272 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -1018,7 +1018,7 @@ class MyStack { } } ``` -> 单对列 +> 单队列 ```php class MyStack { public $queue; @@ -1051,6 +1051,44 @@ class MyStack { } } ``` + +> rust:单队列 + +```rust +struct MyStack { + queue: Vec, +} + +impl MyStack { + fn new() -> Self { + MyStack { queue: vec![] } + } + + fn push(&mut self, x: i32) { + self.queue.push(x); + } + + fn pop(&mut self) -> i32 { + let len = self.queue.len() - 1; + for _ in 0..len { + let tmp = self.queue.remove(0); + self.queue.push(tmp); + } + self.queue.remove(0) + } + + fn top(&mut self) -> i32 { + let res = self.pop(); + self.queue.push(res); + res + } + + fn empty(&self) -> bool { + self.queue.is_empty() + } +} +``` +