Refine some details and coding style for Rust codes (#344)
* Refine some details and coding style for Rust codes * Update coding style for Rust codes * Update time_complexity.rs * Update array.rs * Update leetcode_two_sum.rs * Update hash_map.rs * Update file headers * Update coding style for Rust codes and Zig codes * Update coding style for Rust codes and Zig codes --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
parent
6dc21691ed
commit
e65c7bd478
@ -2,10 +2,7 @@
|
||||
name = "hello-algo-rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
name = "inc"
|
||||
path = "include/include.rs"
|
||||
publish = false
|
||||
|
||||
# Run Command: cargo run --bin time_complexity
|
||||
[[bin]]
|
||||
|
@ -1,8 +1,10 @@
|
||||
/**
|
||||
/*
|
||||
* File: array.rs
|
||||
* Created Time: 2023-01-15
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use rand::Rng;
|
||||
|
||||
@ -45,17 +47,16 @@ fn remove(nums: &mut Vec<i32>, index: usize) {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
/* 遍历数组 */
|
||||
fn traverse(nums: &[i32]) {
|
||||
let mut count = 0;
|
||||
let mut _count = 0;
|
||||
// 通过索引遍历数组
|
||||
for _ in 0..nums.len() {
|
||||
count += 1;
|
||||
_count += 1;
|
||||
}
|
||||
// 直接遍历数组
|
||||
for _ in nums {
|
||||
count += 1;
|
||||
_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,37 +74,37 @@ fn find(nums: &[i32], target: i32) -> Option<usize> {
|
||||
fn main() {
|
||||
let arr = [0; 5];
|
||||
print!("数组 arr = ");
|
||||
inc::print_util::print_array(&arr);
|
||||
print_util::print_array(&arr);
|
||||
// 在 Rust 中,指定长度时([i32; 5])为数组
|
||||
// 由于 Rust 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度
|
||||
// 为了方便实现扩容 extend() 方法,以下将(Vec) 看作数组(Array)也是rust一般情况下使用动态数组的类型
|
||||
let nums = vec![ 1, 3, 2, 5, 4 ];
|
||||
print!("\n数组 nums = ");
|
||||
inc::print_util::print_array(&nums);
|
||||
print_util::print_array(&nums);
|
||||
|
||||
/* 随机访问 */
|
||||
// 随机访问
|
||||
let random_num = random_access(&nums);
|
||||
println!("\n在 nums 中获取随机元素 {}", random_num);
|
||||
|
||||
/* 长度扩展 */
|
||||
// 长度扩展
|
||||
let mut nums = extend(nums, 3);
|
||||
print!("将数组长度扩展至 8 ,得到 nums = ");
|
||||
inc::print_util::print_array(&arr);
|
||||
print_util::print_array(&arr);
|
||||
|
||||
/* 插入元素 */
|
||||
// 插入元素
|
||||
insert(&mut nums, 6, 3);
|
||||
print!("\n在索引 3 处插入数字 6 ,得到 nums = ");
|
||||
inc::print_util::print_array(&nums);
|
||||
print_util::print_array(&nums);
|
||||
|
||||
/* 删除元素 */
|
||||
// 删除元素
|
||||
remove(&mut nums, 2);
|
||||
print!("\n删除索引 2 处的元素,得到 nums = ");
|
||||
inc::print_util::print_array(&nums);
|
||||
print_util::print_array(&nums);
|
||||
|
||||
/* 遍历数组 */
|
||||
// 遍历数组
|
||||
traverse(&nums);
|
||||
|
||||
/* 查找元素 */
|
||||
// 查找元素
|
||||
let index = find(&nums, 3).unwrap();
|
||||
println!("\n在 nums 中查找元素 3 ,得到索引 = {}", index);
|
||||
}
|
@ -1,74 +1,74 @@
|
||||
/**
|
||||
/*
|
||||
* File: list.rs
|
||||
* Created Time: 2023-01-18
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
#[allow(unused_variables)]
|
||||
include!("../include/include.rs");
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* 初始化列表 */
|
||||
// 初始化列表
|
||||
let mut list: Vec<i32> = vec![ 1, 3, 2, 5, 4 ];
|
||||
print!("列表 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
print_util::print_array(&list);
|
||||
|
||||
/* 访问元素 */
|
||||
// 访问元素
|
||||
let num = list[1];
|
||||
println!("\n访问索引 1 处的元素,得到 num = {num}");
|
||||
|
||||
/* 更新元素 */
|
||||
// 更新元素
|
||||
list[1] = 0;
|
||||
print!("将索引 1 处的元素更新为 0 ,得到 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
print_util::print_array(&list);
|
||||
|
||||
/* 清空列表 */
|
||||
// 清空列表
|
||||
list.clear();
|
||||
print!("\n清空列表后 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
print_util::print_array(&list);
|
||||
|
||||
/* 尾部添加元素 */
|
||||
// 尾部添加元素
|
||||
list.push(1);
|
||||
list.push(3);
|
||||
list.push(2);
|
||||
list.push(5);
|
||||
list.push(4);
|
||||
print!("\n添加元素后 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
print_util::print_array(&list);
|
||||
|
||||
/* 中间插入元素 */
|
||||
// 中间插入元素
|
||||
list.insert(3, 6);
|
||||
print!("\n在索引 3 处插入数字 6 ,得到 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
print_util::print_array(&list);
|
||||
|
||||
/* 删除元素 */
|
||||
// 删除元素
|
||||
list.remove(3);
|
||||
print!("\n删除索引 3 处的元素,得到 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
print_util::print_array(&list);
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
let mut count = 0;
|
||||
// 通过索引遍历列表
|
||||
let mut _count = 0;
|
||||
for _ in 0..list.len() {
|
||||
count += 1;
|
||||
_count += 1;
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
// 直接遍历列表元素
|
||||
_count = 0;
|
||||
for _ in &list {
|
||||
count += 1;
|
||||
_count += 1;
|
||||
} // 或者
|
||||
// list.iter().for_each(|_| count += 1);
|
||||
// let count = list.iter().fold(0, |count, _| count + 1);
|
||||
// list.iter().for_each(|_| _count += 1);
|
||||
// let _count = list.iter().fold(0, |_count, _| _count + 1);
|
||||
|
||||
/* 拼接两个列表 */
|
||||
// 拼接两个列表
|
||||
let mut list1 = vec![ 6, 8, 7, 10, 9 ];
|
||||
list.append(&mut list1); // append(移动) 之后 list1 为空!
|
||||
// list.extend(&list1); // extend(借用) list1 能继续使用
|
||||
print!("\n将列表 list1 拼接到 list 之后,得到 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
print_util::print_array(&list);
|
||||
|
||||
/* 排序列表 */
|
||||
// 排序列表
|
||||
list.sort();
|
||||
print!("\n排序列表后 list = ");
|
||||
inc::print_util::print_array(&list);
|
||||
print_util::print_array(&list);
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
/**
|
||||
/*
|
||||
* File: leetcode_two_sum.rs
|
||||
* Created Time: 2023-01-14
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use std::collections::HashMap;
|
||||
struct SolutionBruteForce;
|
||||
@ -49,9 +51,9 @@ fn main() {
|
||||
// 方法一
|
||||
let res = SolutionBruteForce::two_sum(&nums, target);
|
||||
print!("方法一 res = ");
|
||||
inc::print_util::print_array(&res);
|
||||
print_util::print_array(&res);
|
||||
// 方法二
|
||||
let res = SolutionHashMap::two_sum(&nums, target);
|
||||
print!("\n方法二 res = ");
|
||||
inc::print_util::print_array(&res);
|
||||
print_util::print_array(&res);
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
/**
|
||||
/*
|
||||
* File: time_complexity.rs
|
||||
* Created Time: 2023-01-10
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
*/
|
||||
|
||||
/* 常数阶 */
|
||||
fn constant(n: i32) -> i32 {
|
||||
|
@ -1,9 +1,11 @@
|
||||
/**
|
||||
/*
|
||||
* File: time_complexity.rs
|
||||
* Created Time: 2023-01-13
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use rand::seq::SliceRandom;
|
||||
use rand::thread_rng;
|
||||
|
||||
@ -35,7 +37,7 @@
|
||||
let nums = random_numbers(n);
|
||||
let index = find_one(&nums).unwrap();
|
||||
print!("\n数组 [ 1, 2, ..., n ] 被打乱后 = ");
|
||||
inc::print_util::print_array(&nums);
|
||||
print_util::print_array(&nums);
|
||||
println!("\n数字 1 的索引为 {}", index);
|
||||
}
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
/**
|
||||
/*
|
||||
* File: hash_map.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
@ -19,7 +21,7 @@ pub fn main() {
|
||||
map.insert(13276, "小法");
|
||||
map.insert(10583, "小鸭");
|
||||
println!("\n添加完成后,哈希表为\nKey -> Value");
|
||||
inc::print_util::print_hash_map(&map);
|
||||
print_util::print_hash_map(&map);
|
||||
|
||||
// 查询操作
|
||||
// 向哈希表输入键 key ,得到值 value
|
||||
@ -30,11 +32,11 @@ pub fn main() {
|
||||
// 在哈希表中删除键值对 (key, value)
|
||||
_ = map.remove(&10583);
|
||||
println!("\n删除 10583 后,哈希表为\nKey -> Value");
|
||||
inc::print_util::print_hash_map(&map);
|
||||
print_util::print_hash_map(&map);
|
||||
|
||||
// 遍历哈希表
|
||||
println!("\n遍历键值对 Key->Value");
|
||||
inc::print_util::print_hash_map(&map);
|
||||
print_util::print_hash_map(&map);
|
||||
println!("\n单独遍历键 Key");
|
||||
for key in map.keys() {
|
||||
println!("{key}");
|
||||
|
@ -1,8 +1,8 @@
|
||||
/**
|
||||
/*
|
||||
* File: binary_search.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
*/
|
||||
|
||||
/* 二分查找(双闭区间) */
|
||||
fn binary_search(nums: &[i32], target: i32) -> i32 {
|
||||
@ -49,11 +49,11 @@ pub fn main() {
|
||||
let target = 6;
|
||||
let nums = [ 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 ];
|
||||
|
||||
/* 二分查找(双闭区间) */
|
||||
// 二分查找(双闭区间)
|
||||
let mut index = binary_search(&nums, target);
|
||||
println!("目标元素 6 的索引 = {index}");
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
// 二分查找(左闭右开)
|
||||
index = binary_search1(&nums, target);
|
||||
println!("目标元素 6 的索引 = {index}");
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
/**
|
||||
/*
|
||||
* File: bubble_sort.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
/* 冒泡排序 */
|
||||
fn bubble_sort(nums: &mut [i32]) {
|
||||
@ -44,10 +46,10 @@ pub fn main() {
|
||||
let mut nums = [ 4, 1, 3, 1, 5, 2 ];
|
||||
bubble_sort(&mut nums);
|
||||
print!("冒泡排序完成后 nums = ");
|
||||
inc::print_util::print_array(&nums);
|
||||
print_util::print_array(&nums);
|
||||
|
||||
let mut nums1 = [ 4, 1, 3, 1, 5, 2 ];
|
||||
bubble_sort_with_flag(&mut nums1);
|
||||
print!("\n冒泡排序完成后 nums1 = ");
|
||||
inc::print_util::print_array(&nums1);
|
||||
print_util::print_array(&nums1);
|
||||
}
|
@ -1,84 +1,85 @@
|
||||
/**
|
||||
/*
|
||||
* File: array_stack.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: WSL0809 (wslzzy@outlook.com)
|
||||
* Author: WSL0809 (wslzzy@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
use std::vec::Vec;
|
||||
struct ArrayStack {
|
||||
stack: Vec<i32>,
|
||||
include!("../include/include.rs");
|
||||
|
||||
/* 基于数组实现的栈 */
|
||||
struct ArrayStack<T> {
|
||||
stack: Vec<T>,
|
||||
}
|
||||
|
||||
impl ArrayStack {
|
||||
fn new() -> ArrayStack {
|
||||
ArrayStack { stack: Vec::new() }
|
||||
impl<T> ArrayStack<T> {
|
||||
/* 初始化栈 */
|
||||
fn new() -> ArrayStack<T> {
|
||||
ArrayStack::<T> { stack: Vec::<T>::new() }
|
||||
}
|
||||
|
||||
//获取栈的长度
|
||||
/* 获取栈的长度 */
|
||||
fn size(&self) -> usize {
|
||||
self.stack.len()
|
||||
}
|
||||
|
||||
//判断栈是否为空
|
||||
/* 判断栈是否为空 */
|
||||
fn is_empty(&self) -> bool {
|
||||
self.size() == 0
|
||||
}
|
||||
|
||||
//入栈
|
||||
fn push(&mut self, num: i32) {
|
||||
/* 入栈 */
|
||||
fn push(&mut self, num: T) {
|
||||
self.stack.push(num);
|
||||
}
|
||||
|
||||
//出栈
|
||||
fn pop(&mut self) -> i32 {
|
||||
/* 出栈 */
|
||||
fn pop(&mut self) -> Option<T> {
|
||||
match self.stack.pop() {
|
||||
Some(num) => num,
|
||||
None => panic!("stack is empty"),
|
||||
Some(num) => Some(num),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
//访问栈顶元素
|
||||
fn peek(&self) -> i32 {
|
||||
*self
|
||||
.stack
|
||||
.last()
|
||||
.unwrap_or_else(|| panic!("stack is empty"))
|
||||
/* 访问栈顶元素 */
|
||||
fn peek(&self) -> Option<&T> {
|
||||
if self.is_empty() { panic!("栈为空") };
|
||||
self.stack.last()
|
||||
}
|
||||
|
||||
fn to_vec(&self) -> Vec<i32> {
|
||||
self.stack.clone()
|
||||
/* 返回 &Vec */
|
||||
fn to_array(&self) -> &Vec<T> {
|
||||
&self.stack
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
//初始化栈
|
||||
let mut stack = ArrayStack::new();
|
||||
// 初始化栈
|
||||
let mut stack = ArrayStack::<i32>::new();
|
||||
|
||||
//元素入栈
|
||||
// 元素入栈
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
println!("栈 stack = {:?}", stack.to_vec());
|
||||
print!("栈 stack = ");
|
||||
print_util::print_array(stack.to_array());
|
||||
|
||||
//访问栈顶元素
|
||||
let peek = stack.peek();
|
||||
println!("栈顶元素 peek = {}", peek);
|
||||
let peek = stack.peek().unwrap();
|
||||
print!("\n栈顶元素 peek = {}", peek);
|
||||
|
||||
//元素出栈
|
||||
let pop = stack.pop();
|
||||
println!(
|
||||
"出栈元素 pop = {},出栈后 stack = {:?}",
|
||||
pop,
|
||||
stack.to_vec()
|
||||
);
|
||||
// 元素出栈
|
||||
let pop = stack.pop().unwrap();
|
||||
print!("\n出栈元素 pop = {pop},出栈后 stack = ");
|
||||
print_util::print_array(stack.to_array());
|
||||
|
||||
//获取栈的长度
|
||||
// 获取栈的长度
|
||||
let size = stack.size();
|
||||
println!("栈的长度 size = {}", size);
|
||||
print!("\n栈的长度 size = {size}");
|
||||
|
||||
//判断是否为空
|
||||
// 判断是否为空
|
||||
let is_empty = stack.is_empty();
|
||||
println!("栈是否为空 = {}", is_empty);
|
||||
print!("\n栈是否为空 = {is_empty}");
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
/**
|
||||
/*
|
||||
* File: deque.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use std::collections::LinkedList;
|
||||
|
||||
@ -16,7 +18,7 @@ pub fn main() {
|
||||
deque.push_front(3); // 添加至队首
|
||||
deque.push_front(1);
|
||||
print!("双向队列 deque = ");
|
||||
inc::print_util::print_queue(&deque);
|
||||
print_util::print_queue(&deque);
|
||||
|
||||
// 访问元素
|
||||
let peek_first = deque.front().unwrap(); // 队首元素
|
||||
@ -27,16 +29,16 @@ pub fn main() {
|
||||
// 元素出队
|
||||
let poll_first = deque.pop_front().unwrap(); // 队首元素出队
|
||||
print!("\n队首出队元素 pollFirst = {poll_first},队首出队后 deque = ");
|
||||
inc::print_util::print_queue(&deque);
|
||||
print_util::print_queue(&deque);
|
||||
let poll_last = deque.pop_back().unwrap(); // 队尾元素出队
|
||||
print!("\n队尾出队元素 pollLast = {poll_last},队尾出队后 deque = ");
|
||||
inc::print_util::print_queue(&deque);
|
||||
print_util::print_queue(&deque);
|
||||
|
||||
// 获取双向队列的长度
|
||||
let size = deque.len();
|
||||
print!("\n双向队列长度 size = {size}");
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
// 判断双向队列是否为空
|
||||
let is_empty = deque.is_empty();
|
||||
print!("\n双向队列是否为空 = {is_empty}");
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
/**
|
||||
/*
|
||||
* File: queue.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use std::collections::LinkedList;
|
||||
|
||||
@ -18,7 +20,7 @@ pub fn main() {
|
||||
queue.push_back(5);
|
||||
queue.push_back(4);
|
||||
print!("队列 queue = ");
|
||||
inc::print_util::print_queue(&queue);
|
||||
print_util::print_queue(&queue);
|
||||
|
||||
// 访问队首元素
|
||||
let peek = queue.front().unwrap();
|
||||
@ -27,13 +29,13 @@ pub fn main() {
|
||||
// 元素出队
|
||||
let poll = queue.pop_front().unwrap();
|
||||
print!("出队元素 poll = {poll},出队后 queue = ");
|
||||
inc::print_util::print_queue(&queue);
|
||||
print_util::print_queue(&queue);
|
||||
|
||||
// 获取队列的长度
|
||||
let size = queue.len();
|
||||
print!("\n队列长度 size = {size}");
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
// 判断队列是否为空
|
||||
let is_empty = queue.is_empty();
|
||||
print!("\n队列是否为空 = {is_empty}");
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
/**
|
||||
/*
|
||||
* File: stack.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
@ -17,7 +19,7 @@ pub fn main() {
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
print!("栈 stack = ");
|
||||
inc::print_util::print_array(&stack);
|
||||
print_util::print_array(&stack);
|
||||
|
||||
// 访问栈顶元素
|
||||
let peek = stack.get(stack.len() - 1).unwrap();
|
||||
@ -26,7 +28,7 @@ pub fn main() {
|
||||
// 元素出栈
|
||||
let pop = stack.pop().unwrap();
|
||||
print!("\n出栈元素 pop = {pop},出栈后 stack = ");
|
||||
inc::print_util::print_array(&stack);
|
||||
print_util::print_array(&stack);
|
||||
|
||||
// 获取栈的长度
|
||||
let size = stack.len();
|
||||
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
/*
|
||||
* File: include.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
*/
|
||||
|
||||
pub mod print_util;
|
@ -1,12 +1,11 @@
|
||||
/**
|
||||
/*
|
||||
* File: print_util.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
*/
|
||||
|
||||
use std::fmt::Display;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::LinkedList;
|
||||
use std::collections::{HashMap, LinkedList};
|
||||
|
||||
/* Print an array */
|
||||
pub fn print_array<T: Display>(nums: &[T]) {
|
||||
|
@ -154,7 +154,7 @@ pub fn build(b: *std.build.Builder) void {
|
||||
exe_array_stack.setTarget(target);
|
||||
exe_array_stack.setBuildMode(mode);
|
||||
exe_array_stack.install();
|
||||
const run_cmd_array_stack = exe_linkedlist_stack.run();
|
||||
const run_cmd_array_stack = exe_array_stack.run();
|
||||
run_cmd_array_stack.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd_array_stack.addArgs(args);
|
||||
const run_step_array_stack = b.step("run_array_stack", "Run array_stack");
|
||||
|
@ -68,8 +68,8 @@ pub fn main() !void {
|
||||
std.debug.print("\n堆元素数量为 {}\n", .{size});
|
||||
|
||||
// 判断堆是否为空
|
||||
var isEmpty = if (maxHeap.len == 0) true else false;
|
||||
std.debug.print("\n堆是否为空 {}\n", .{isEmpty});
|
||||
var is_empty = if (maxHeap.len == 0) true else false;
|
||||
std.debug.print("\n堆是否为空 {}\n", .{is_empty});
|
||||
|
||||
// 输入列表并建堆
|
||||
try minHeap.addSlice(&[_]i32{ 1, 3, 2, 5, 4 });
|
||||
|
@ -31,13 +31,13 @@ pub fn ArrayStack(comptime T: type) type {
|
||||
}
|
||||
|
||||
// 判断栈是否为空
|
||||
pub fn empty(self: *Self) bool {
|
||||
pub fn isEmpty(self: *Self) bool {
|
||||
return self.size() == 0;
|
||||
}
|
||||
|
||||
// 访问栈顶元素
|
||||
pub fn top(self: *Self) T {
|
||||
if (self.size() == 0) @panic("栈为空");
|
||||
pub fn peek(self: *Self) T {
|
||||
if (self.isEmpty()) @panic("栈为空");
|
||||
return self.stack.?.items[self.size() - 1];
|
||||
}
|
||||
|
||||
@ -77,7 +77,7 @@ pub fn main() !void {
|
||||
inc.PrintUtil.printList(i32, stack.toList());
|
||||
|
||||
// 访问栈顶元素
|
||||
var peek = stack.top();
|
||||
var peek = stack.peek();
|
||||
std.debug.print("\n栈顶元素 peek = {}", .{peek});
|
||||
|
||||
// 元素出栈
|
||||
@ -90,7 +90,7 @@ pub fn main() !void {
|
||||
std.debug.print("\n栈的长度 size = {}", .{size});
|
||||
|
||||
// 判断栈是否为空
|
||||
var is_empty = stack.empty();
|
||||
var is_empty = stack.isEmpty();
|
||||
std.debug.print("\n栈是否为空 = {}", .{is_empty});
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
|
@ -37,7 +37,7 @@ pub fn LinkedListStack(comptime T: type) type {
|
||||
}
|
||||
|
||||
// 判断栈是否为空
|
||||
pub fn empty(self: *Self) bool {
|
||||
pub fn isEmpty(self: *Self) bool {
|
||||
return self.size() == 0;
|
||||
}
|
||||
|
||||
@ -110,7 +110,7 @@ pub fn main() !void {
|
||||
std.debug.print("\n栈的长度 size = {}", .{size});
|
||||
|
||||
// 判断栈是否为空
|
||||
var is_empty = stack.empty();
|
||||
var is_empty = stack.isEmpty();
|
||||
std.debug.print("\n栈是否为空 = {}", .{is_empty});
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
|
@ -36,8 +36,8 @@ pub fn main() !void {
|
||||
std.debug.print("\n栈的长度 size = {}", .{size});
|
||||
|
||||
// 判断栈是否为空
|
||||
var empty = if (stack.items.len == 0) true else false;
|
||||
std.debug.print("\n栈是否为空 = {}", .{empty});
|
||||
var is_empty = if (stack.items.len == 0) true else false;
|
||||
std.debug.print("\n栈是否为空 = {}", .{is_empty});
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user