mirror of
https://github.com/krahets/hello-algo.git
synced 2025-02-02 22:43:50 +08:00
feat: add Rust code in stack.md (#748)
* Update stack.md Add rust sample code * Update stack.md * Update stack.md --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
parent
eba015c0bb
commit
0a2ad4489a
@ -266,7 +266,32 @@
|
|||||||
=== "Rust"
|
=== "Rust"
|
||||||
|
|
||||||
```rust title="stack.rs"
|
```rust title="stack.rs"
|
||||||
|
/* 初始化栈 */
|
||||||
|
// 把 Vec 当作栈来使用
|
||||||
|
let mut stack: Vec<i32> = Vec::new();
|
||||||
|
|
||||||
|
/* 元素入栈 */
|
||||||
|
stack.push(1);
|
||||||
|
stack.push(3);
|
||||||
|
stack.push(2);
|
||||||
|
stack.push(5);
|
||||||
|
stack.push(4);
|
||||||
|
|
||||||
|
/* 访问栈顶元素 */
|
||||||
|
if let Some(top) = stack.get(stack.len() - 1) {
|
||||||
|
}
|
||||||
|
if let Some(top) = stack.last() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 元素出栈 */
|
||||||
|
if let Some(pop) = stack.pop() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 获取栈的长度 */
|
||||||
|
let size = stack.len();
|
||||||
|
|
||||||
|
/* 判断是否为空 */
|
||||||
|
let isEmpty = stack.is_empty();
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C"
|
=== "C"
|
||||||
|
Loading…
Reference in New Issue
Block a user