mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00:36 +08:00
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
41 lines
1.0 KiB
Rust
41 lines
1.0 KiB
Rust
/*
|
|
* File: stack.rs
|
|
* Created Time: 2023-02-05
|
|
* Author: codingonion (coderonion@gmail.com)
|
|
*/
|
|
|
|
use hello_algo_rust::include::print_util;
|
|
|
|
/* Driver code */
|
|
pub fn main() {
|
|
// Initialize the stack
|
|
// In Rust, Vec is the recommended type for implementing a stack
|
|
let mut stack: Vec<i32> = Vec::new();
|
|
|
|
// Push elements onto the stack
|
|
stack.push(1);
|
|
stack.push(3);
|
|
stack.push(2);
|
|
stack.push(5);
|
|
stack.push(4);
|
|
print!("Stack stack = ");
|
|
print_util::print_array(&stack);
|
|
|
|
// Access the top element
|
|
let peek = stack.last().unwrap();
|
|
print!("\nTop element peek = {peek}");
|
|
|
|
// Pop an element from the stack
|
|
let pop = stack.pop().unwrap();
|
|
print!("\nPopped element pop = {pop}; after popping, stack = ");
|
|
print_util::print_array(&stack);
|
|
|
|
// Get the stack size
|
|
let size = stack.len();
|
|
print!("\nStack size = {size}");
|
|
|
|
// Check whether the stack is empty
|
|
let is_empty = stack.is_empty();
|
|
print!("\nIs the stack empty? {is_empty}");
|
|
}
|