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"