mirror of
https://github.com/krahets/hello-algo.git
synced 2025-01-23 06:00:27 +08:00
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
This commit is contained in:
parent
0a2ad4489a
commit
b2246e11a7
@ -301,7 +301,33 @@
|
||||
=== "Rust"
|
||||
|
||||
```rust title="deque.rs"
|
||||
/* 初始化双向队列 */
|
||||
let mut deque: VecDeque<u32> = 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"
|
||||
|
@ -268,7 +268,30 @@
|
||||
=== "Rust"
|
||||
|
||||
```rust title="queue.rs"
|
||||
/* 初始化双向队列 */
|
||||
// 在 Rust 中使用双向队列作为普通队列来使用
|
||||
let mut deque: VecDeque<u32> = 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"
|
||||
|
Loading…
Reference in New Issue
Block a user