From e65c7bd478df3ee1a8549618ecf89a5c5eb63464 Mon Sep 17 00:00:00 2001 From: sjinzh <99076655+sjinzh@users.noreply.github.com> Date: Thu, 9 Feb 2023 00:25:01 +0800 Subject: [PATCH] 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 --- codes/rust/Cargo.toml | 5 +- .../chapter_array_and_linkedlist/array.rs | 37 +++--- .../rust/chapter_array_and_linkedlist/list.rs | 112 +++++++++--------- .../leetcode_two_sum.rs | 12 +- .../time_complexity.rs | 6 +- .../worst_best_time_complexity.rs | 6 +- codes/rust/chapter_hashing/hash_map.rs | 14 ++- codes/rust/chapter_searching/binary_search.rs | 8 +- codes/rust/chapter_sorting/bubble_sort.rs | 10 +- .../chapter_stack_and_queue/array_stack.rs | 85 ++++++------- codes/rust/chapter_stack_and_queue/deque.rs | 14 ++- codes/rust/chapter_stack_and_queue/queue.rs | 12 +- codes/rust/chapter_stack_and_queue/stack.rs | 10 +- codes/rust/include/include.rs | 4 +- codes/rust/include/print_util.rs | 7 +- codes/zig/build.zig | 2 +- codes/zig/chapter_heap/heap.zig | 4 +- .../chapter_stack_and_queue/array_stack.zig | 10 +- .../linkedlist_stack.zig | 4 +- codes/zig/chapter_stack_and_queue/stack.zig | 4 +- 20 files changed, 189 insertions(+), 177 deletions(-) diff --git a/codes/rust/Cargo.toml b/codes/rust/Cargo.toml index acdda7fb..2775ec93 100644 --- a/codes/rust/Cargo.toml +++ b/codes/rust/Cargo.toml @@ -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]] diff --git a/codes/rust/chapter_array_and_linkedlist/array.rs b/codes/rust/chapter_array_and_linkedlist/array.rs index b4779874..a395eec6 100644 --- a/codes/rust/chapter_array_and_linkedlist/array.rs +++ b/codes/rust/chapter_array_and_linkedlist/array.rs @@ -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, 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 { 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); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_array_and_linkedlist/list.rs b/codes/rust/chapter_array_and_linkedlist/list.rs index 46187be9..997d7fbc 100644 --- a/codes/rust/chapter_array_and_linkedlist/list.rs +++ b/codes/rust/chapter_array_and_linkedlist/list.rs @@ -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 = vec![ 1, 3, 2, 5, 4 ]; - print!("列表 list = "); - inc::print_util::print_array(&list); + // 初始化列表 + let mut list: Vec = vec![ 1, 3, 2, 5, 4 ]; + print!("列表 list = "); + print_util::print_array(&list); - /* 访问元素 */ - let num = list[1]; - println!("\n访问索引 1 处的元素,得到 num = {num}"); + // 访问元素 + let num = list[1]; + println!("\n访问索引 1 处的元素,得到 num = {num}"); - /* 更新元素 */ - list[1] = 0; - print!("将索引 1 处的元素更新为 0 ,得到 list = "); - inc::print_util::print_array(&list); + // 更新元素 + list[1] = 0; + print!("将索引 1 处的元素更新为 0 ,得到 list = "); + print_util::print_array(&list); - /* 清空列表 */ - list.clear(); - print!("\n清空列表后 list = "); - inc::print_util::print_array(&list); + // 清空列表 + list.clear(); + print!("\n清空列表后 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); + // 尾部添加元素 + list.push(1); + list.push(3); + list.push(2); + list.push(5); + list.push(4); + print!("\n添加元素后 list = "); + print_util::print_array(&list); - /* 中间插入元素 */ - list.insert(3, 6); - print!("\n在索引 3 处插入数字 6 ,得到 list = "); - inc::print_util::print_array(&list); + // 中间插入元素 + list.insert(3, 6); + print!("\n在索引 3 处插入数字 6 ,得到 list = "); + print_util::print_array(&list); - /* 删除元素 */ - list.remove(3); - print!("\n删除索引 3 处的元素,得到 list = "); - inc::print_util::print_array(&list); + // 删除元素 + list.remove(3); + print!("\n删除索引 3 处的元素,得到 list = "); + print_util::print_array(&list); - /* 通过索引遍历列表 */ - let mut count = 0; - for _ in 0..list.len() { - count += 1; - } + // 通过索引遍历列表 + let mut _count = 0; + for _ in 0..list.len() { + _count += 1; + } - /* 直接遍历列表元素 */ - count = 0; - for _ in &list { - count += 1; - } // 或者 - // list.iter().for_each(|_| count += 1); - // let count = list.iter().fold(0, |count, _| count + 1); + // 直接遍历列表元素 + _count = 0; + for _ in &list { + _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); + // 拼接两个列表 + 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 = "); + print_util::print_array(&list); - /* 排序列表 */ - list.sort(); - print!("\n排序列表后 list = "); - inc::print_util::print_array(&list); + // 排序列表 + list.sort(); + print!("\n排序列表后 list = "); + print_util::print_array(&list); } \ No newline at end of file diff --git a/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs b/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs index 9b5b036b..7fddae34 100644 --- a/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs +++ b/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs @@ -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); -} \ No newline at end of file + print_util::print_array(&res); +} diff --git a/codes/rust/chapter_computational_complexity/time_complexity.rs b/codes/rust/chapter_computational_complexity/time_complexity.rs index 9f24db7c..f7c2b6b0 100644 --- a/codes/rust/chapter_computational_complexity/time_complexity.rs +++ b/codes/rust/chapter_computational_complexity/time_complexity.rs @@ -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 { @@ -167,4 +167,4 @@ fn main() { count = factorial_recur(n); println!("阶乘阶(递归实现)的计算操作数量 = {}", count); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs b/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs index 706b70f5..6a05fac6 100644 --- a/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs +++ b/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs @@ -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); } } \ No newline at end of file diff --git a/codes/rust/chapter_hashing/hash_map.rs b/codes/rust/chapter_hashing/hash_map.rs index 562a349d..3fa2dc5f 100644 --- a/codes/rust/chapter_hashing/hash_map.rs +++ b/codes/rust/chapter_hashing/hash_map.rs @@ -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}"); @@ -43,4 +45,4 @@ pub fn main() { for value in map.values() { println!("{value}"); } -} \ No newline at end of file +} diff --git a/codes/rust/chapter_searching/binary_search.rs b/codes/rust/chapter_searching/binary_search.rs index 1c2edcdb..ada931b7 100644 --- a/codes/rust/chapter_searching/binary_search.rs +++ b/codes/rust/chapter_searching/binary_search.rs @@ -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}"); } \ No newline at end of file diff --git a/codes/rust/chapter_sorting/bubble_sort.rs b/codes/rust/chapter_sorting/bubble_sort.rs index 3c8e294e..b4957f7e 100644 --- a/codes/rust/chapter_sorting/bubble_sort.rs +++ b/codes/rust/chapter_sorting/bubble_sort.rs @@ -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); } \ No newline at end of file diff --git a/codes/rust/chapter_stack_and_queue/array_stack.rs b/codes/rust/chapter_stack_and_queue/array_stack.rs index f9776412..8fb60509 100644 --- a/codes/rust/chapter_stack_and_queue/array_stack.rs +++ b/codes/rust/chapter_stack_and_queue/array_stack.rs @@ -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, +include!("../include/include.rs"); + +/* 基于数组实现的栈 */ +struct ArrayStack { + stack: Vec, } -impl ArrayStack { - fn new() -> ArrayStack { - ArrayStack { stack: Vec::new() } +impl ArrayStack { + /* 初始化栈 */ + fn new() -> ArrayStack { + ArrayStack:: { stack: Vec::::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 { 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 { - self.stack.clone() + /* 返回 &Vec */ + fn to_array(&self) -> &Vec { + &self.stack } } +/* Driver Code */ fn main() { - //初始化栈 - let mut stack = ArrayStack::new(); + // 初始化栈 + let mut stack = ArrayStack::::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}"); +} \ No newline at end of file diff --git a/codes/rust/chapter_stack_and_queue/deque.rs b/codes/rust/chapter_stack_and_queue/deque.rs index 6cb12c0f..8bb8ad42 100644 --- a/codes/rust/chapter_stack_and_queue/deque.rs +++ b/codes/rust/chapter_stack_and_queue/deque.rs @@ -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}"); } \ No newline at end of file diff --git a/codes/rust/chapter_stack_and_queue/queue.rs b/codes/rust/chapter_stack_and_queue/queue.rs index 54a7597f..014b5a5d 100644 --- a/codes/rust/chapter_stack_and_queue/queue.rs +++ b/codes/rust/chapter_stack_and_queue/queue.rs @@ -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}"); } \ No newline at end of file diff --git a/codes/rust/chapter_stack_and_queue/stack.rs b/codes/rust/chapter_stack_and_queue/stack.rs index 520f7b07..4fd7c1e6 100644 --- a/codes/rust/chapter_stack_and_queue/stack.rs +++ b/codes/rust/chapter_stack_and_queue/stack.rs @@ -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(); diff --git a/codes/rust/include/include.rs b/codes/rust/include/include.rs index 2e9cdecf..1363cb9f 100644 --- a/codes/rust/include/include.rs +++ b/codes/rust/include/include.rs @@ -1,7 +1,7 @@ -/** +/* * File: include.rs * Created Time: 2023-02-05 * Author: sjinzh (sjinzh@gmail.com) -*/ + */ pub mod print_util; \ No newline at end of file diff --git a/codes/rust/include/print_util.rs b/codes/rust/include/print_util.rs index 8800fea3..ccf1068a 100644 --- a/codes/rust/include/print_util.rs +++ b/codes/rust/include/print_util.rs @@ -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(nums: &[T]) { diff --git a/codes/zig/build.zig b/codes/zig/build.zig index 0e4edad8..b061190a 100644 --- a/codes/zig/build.zig +++ b/codes/zig/build.zig @@ -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"); diff --git a/codes/zig/chapter_heap/heap.zig b/codes/zig/chapter_heap/heap.zig index 0d4b5b44..ecc3fe84 100644 --- a/codes/zig/chapter_heap/heap.zig +++ b/codes/zig/chapter_heap/heap.zig @@ -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 }); diff --git a/codes/zig/chapter_stack_and_queue/array_stack.zig b/codes/zig/chapter_stack_and_queue/array_stack.zig index 0c6d25a0..77f230a5 100644 --- a/codes/zig/chapter_stack_and_queue/array_stack.zig +++ b/codes/zig/chapter_stack_and_queue/array_stack.zig @@ -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(); diff --git a/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig b/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig index 38defc1b..8fd8f783 100644 --- a/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig +++ b/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig @@ -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(); diff --git a/codes/zig/chapter_stack_and_queue/stack.zig b/codes/zig/chapter_stack_and_queue/stack.zig index 9bd076e9..300c2d83 100644 --- a/codes/zig/chapter_stack_and_queue/stack.zig +++ b/codes/zig/chapter_stack_and_queue/stack.zig @@ -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(); }