From b2246e11a7e9c11baad1a404f4767837b86b12b7 Mon Sep 17 00:00:00 2001 From: hts0000 <52144113+hts0000@users.noreply.github.com> Date: Sun, 17 Sep 2023 00:39:57 +0800 Subject: [PATCH] feat: add Rust code in queue.md and deque.md (#757) * feat: add Rust code in queue.md * feat: add Rust code in deque.md * style: fix comment style --- docs/chapter_stack_and_queue/deque.md | 26 ++++++++++++++++++++++++++ docs/chapter_stack_and_queue/queue.md | 23 +++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/docs/chapter_stack_and_queue/deque.md b/docs/chapter_stack_and_queue/deque.md index da55dd125..1e8ec7767 100644 --- a/docs/chapter_stack_and_queue/deque.md +++ b/docs/chapter_stack_and_queue/deque.md @@ -301,7 +301,33 @@ === "Rust" ```rust title="deque.rs" + /* 初始化双向队列 */ + let mut deque: VecDeque = VecDeque::new(); + /* 元素入队 */ + deque.push_back(2); // 添加至队尾 + deque.push_back(5); + deque.push_back(4); + deque.push_front(3); // 添加至队首 + deque.push_front(1); + + /* 访问元素 */ + if let Some(front) = deque.front() { // 队首元素 + } + if let Some(rear) = deque.back() { // 队尾元素 + } + + /* 元素出队 */ + if let Some(pop_front) = deque.pop_front() { // 队首元素出队 + } + if let Some(pop_rear) = deque.pop_back() { // 队尾元素出队 + } + + /* 获取双向队列的长度 */ + let size = deque.len(); + + /* 判断双向队列是否为空 */ + let is_empty = deque.is_empty(); ``` === "C" diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index 17e5f3a7b..ed5b92011 100755 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -268,7 +268,30 @@ === "Rust" ```rust title="queue.rs" + /* 初始化双向队列 */ + // 在 Rust 中使用双向队列作为普通队列来使用 + let mut deque: VecDeque = VecDeque::new(); + /* 元素入队 */ + deque.push_back(1); + deque.push_back(3); + deque.push_back(2); + deque.push_back(5); + deque.push_back(4); + + /* 访问队首元素 */ + if let Some(front) = deque.front() { + } + + /* 元素出队 */ + if let Some(pop) = deque.pop_front() { + } + + /* 获取队列的长度 */ + let size = deque.len(); + + /* 判断队列是否为空 */ + let is_empty = deque.is_empty(); ``` === "C"