mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 15:36:29 +08:00
8a388d8422
* update rust codes * update rust codes * update rust codes * update and add rust codes for hash_map, binary_search, bubble_sort * update and add rust codes for hash_map, binary_search, bubble_sort * add rust codes for chapter stack * add rust codes for chapter queue * add rust codes for chapter deque
38 lines
No EOL
926 B
Rust
38 lines
No EOL
926 B
Rust
/**
|
|
* File: stack.rs
|
|
* Created Time: 2023-02-05
|
|
* Author: sjinzh (sjinzh@gmail.com)
|
|
*/
|
|
|
|
/* Driver Code */
|
|
pub fn main() {
|
|
// 初始化栈
|
|
// 在 rust 中,推荐将 Vec 当作栈来使用
|
|
let mut stack: Vec<i32> = Vec::new();
|
|
|
|
// 元素入栈
|
|
stack.push(1);
|
|
stack.push(3);
|
|
stack.push(2);
|
|
stack.push(5);
|
|
stack.push(4);
|
|
print!("栈 stack = ");
|
|
inc::print_util::print_array(&stack);
|
|
|
|
// 访问栈顶元素
|
|
let peek = stack.get(stack.len() - 1).unwrap();
|
|
print!("\n栈顶元素 peek = {peek}");
|
|
|
|
// 元素出栈
|
|
let pop = stack.pop().unwrap();
|
|
print!("\n出栈元素 pop = {pop},出栈后 stack = ");
|
|
inc::print_util::print_array(&stack);
|
|
|
|
// 获取栈的长度
|
|
let size = stack.len();
|
|
print!("\n栈的长度 size = {size}");
|
|
|
|
// 判断栈是否为空
|
|
let is_empty = stack.is_empty();
|
|
print!("\n栈是否为空 = {is_empty}");
|
|
} |