Skip to content

Commit

Permalink
Update the coding style for Zig (krahets#336)
Browse files Browse the repository at this point in the history
* Update the coding style for Zig

* Update array.rs
  • Loading branch information
coderonion authored Feb 5, 2023
1 parent cb73007 commit 0635010
Show file tree
Hide file tree
Showing 26 changed files with 40 additions and 61 deletions.
4 changes: 3 additions & 1 deletion codes/rust/chapter_array_and_linkedlist/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
* Author: xBLACICEx ([email protected]), sjinzh ([email protected])
*/

use rand::Rng;

/* 随机返回一个数组元素 */
fn random_access(nums: &[i32]) -> i32 {
// 在区间 [0, nums.len()) 中随机抽取一个数字
let random_index = rand::random::<usize>() % nums.len();
let random_index = rand::thread_rng().gen_range(0..nums.len());
// 获取并返回随机元素
let random_num = nums[random_index];
random_num
Expand Down
3 changes: 1 addition & 2 deletions codes/zig/chapter_array_and_linkedlist/array.zig
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ pub fn main() !void {
var index = find(nums, 3);
std.debug.print("\n在 nums 中查找元素 3 ,得到索引 = {}\n", .{index});

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}

3 changes: 1 addition & 2 deletions codes/zig/chapter_array_and_linkedlist/linked_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,5 @@ pub fn main() !void {
var index = find(&n0, 2);
std.debug.print("链表中值为 2 的结点的索引 = {}\n", .{index});

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}
3 changes: 1 addition & 2 deletions codes/zig/chapter_array_and_linkedlist/list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ pub fn main() !void {
std.debug.print("\n排序列表后 list = ", .{});
inc.PrintUtil.printList(i32, list);

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}

4 changes: 1 addition & 3 deletions codes/zig/chapter_array_and_linkedlist/my_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const std = @import("std");
const inc = @import("include");

// 列表类简易实现
// 编译期泛型
pub fn MyList(comptime T: type) type {
return struct {
const Self = @This();
Expand Down Expand Up @@ -171,7 +170,6 @@ pub fn main() !void {
inc.PrintUtil.printArray(i32, try list.toArray());
std.debug.print(" ,容量 = {} ,长度 = {}\n", .{list.capacity(), list.size()});

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}

Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,5 @@ pub fn main() !void {
};
try inc.PrintUtil.printTree(root, null, false);

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}
3 changes: 1 addition & 2 deletions codes/zig/chapter_hashing/array_hash_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,5 @@ pub fn main() !void {
}
value_set.deinit();

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}
3 changes: 1 addition & 2 deletions codes/zig/chapter_hashing/hash_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ pub fn main() !void {
std.debug.print("{s}\n", .{kv.value_ptr.*});
}

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}

3 changes: 1 addition & 2 deletions codes/zig/chapter_heap/heap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,5 @@ pub fn main() !void {
std.debug.print("\n输入列表并建立小顶堆后\n", .{});
try inc.PrintUtil.printHeap(i32, mem_allocator, minHeap);

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}
8 changes: 3 additions & 5 deletions codes/zig/chapter_heap/my_heap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const std = @import("std");
const inc = @import("include");

// 堆类简易实现
// 编译期泛型
pub fn MaxHeap(comptime T: type) type {
return struct {
const Self = @This();
Expand Down Expand Up @@ -181,10 +180,9 @@ pub fn main() !void {
std.debug.print("\n堆元素数量为 {}", .{size});

// 判断堆是否为空
var isEmpty = maxHeap.isEmpty();
std.debug.print("\n堆是否为空 {}\n", .{isEmpty});
var is_empty = maxHeap.isEmpty();
std.debug.print("\n堆是否为空 {}\n", .{is_empty});

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}

3 changes: 1 addition & 2 deletions codes/zig/chapter_searching/linear_search.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub fn main() !void {
std.debug.print("目标结点值 3 的对应结点对象为 ", .{});
try inc.PrintUtil.printLinkedList(i32, node);

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}

3 changes: 1 addition & 2 deletions codes/zig/chapter_sorting/bubble_sort.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub fn main() !void {
std.debug.print("\n冒泡排序完成后 nums1 = ", .{});
inc.PrintUtil.printArray(i32, &nums1);

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}

3 changes: 1 addition & 2 deletions codes/zig/chapter_sorting/insertion_sort.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub fn main() !void {
std.debug.print("插入排序完成后 nums = ", .{});
inc.PrintUtil.printArray(i32, &nums);

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}

4 changes: 2 additions & 2 deletions codes/zig/chapter_stack_and_queue/array_queue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ pub fn main() !void {
std.debug.print("\n队列长度 size = {}", .{size});

// 判断队列是否为空
var isEmpty = queue.isEmpty();
std.debug.print("\n队列是否为空 = {}", .{isEmpty});
var is_empty = queue.isEmpty();
std.debug.print("\n队列是否为空 = {}", .{is_empty});

// 测试环形数组
var i: i32 = 0;
Expand Down
14 changes: 6 additions & 8 deletions codes/zig/chapter_stack_and_queue/array_stack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const std = @import("std");
const inc = @import("include");

// 基于数组实现的栈
// 编译期泛型
pub fn ArrayStack(comptime T: type) type {
return struct {
const Self = @This();
Expand Down Expand Up @@ -78,11 +77,11 @@ pub fn main() !void {
inc.PrintUtil.printList(i32, stack.toList());

// 访问栈顶元素
var top = stack.top();
std.debug.print("\n栈顶元素 top = {}", .{top});
var peek = stack.top();
std.debug.print("\n栈顶元素 peek = {}", .{peek});

// 元素出栈
top = stack.pop();
var top = stack.pop();
std.debug.print("\n出栈元素 pop = {},出栈后 stack = ", .{top});
inc.PrintUtil.printList(i32, stack.toList());

Expand All @@ -91,9 +90,8 @@ pub fn main() !void {
std.debug.print("\n栈的长度 size = {}", .{size});

// 判断栈是否为空
var empty = stack.empty();
std.debug.print("\n栈是否为空 = {}", .{empty});
var is_empty = stack.empty();
std.debug.print("\n栈是否为空 = {}", .{is_empty});

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}
4 changes: 2 additions & 2 deletions codes/zig/chapter_stack_and_queue/deque.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub fn main() !void {
std.debug.print("\n双向队列长度 size = {}", .{size});

// 判断双向队列是否为空
var isEmpty = if (deque.len == 0) true else false;
std.debug.print("\n双向队列是否为空 = {}", .{isEmpty});
var is_empty = if (deque.len == 0) true else false;
std.debug.print("\n双向队列是否为空 = {}", .{is_empty});

_ = try std.io.getStdIn().reader().readByte();
}
4 changes: 2 additions & 2 deletions codes/zig/chapter_stack_and_queue/linkedlist_deque.zig
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ pub fn main() !void {
std.debug.print("\n队列长度 size = {}", .{size});

// 判断双向队列是否为空
var isEmpty = deque.isEmpty();
std.debug.print("\n双向队列是否为空 = {}", .{isEmpty});
var is_empty = deque.isEmpty();
std.debug.print("\n双向队列是否为空 = {}", .{is_empty});

_ = try std.io.getStdIn().reader().readByte();
}
4 changes: 2 additions & 2 deletions codes/zig/chapter_stack_and_queue/linkedlist_queue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ pub fn main() !void {
std.debug.print("\n队列长度 size = {}", .{size});

// 判断队列是否为空
var isEmpty = queue.isEmpty();
std.debug.print("\n队列是否为空 = {}", .{isEmpty});
var is_empty = queue.isEmpty();
std.debug.print("\n队列是否为空 = {}", .{is_empty});

_ = try std.io.getStdIn().reader().readByte();
}
8 changes: 3 additions & 5 deletions codes/zig/chapter_stack_and_queue/linkedlist_stack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const std = @import("std");
const inc = @import("include");

// 基于链表实现的栈
// 编译期泛型
pub fn LinkedListStack(comptime T: type) type {
return struct {
const Self = @This();
Expand Down Expand Up @@ -111,10 +110,9 @@ pub fn main() !void {
std.debug.print("\n栈的长度 size = {}", .{size});

// 判断栈是否为空
var empty = stack.empty();
std.debug.print("\n栈是否为空 = {}", .{empty});
var is_empty = stack.empty();
std.debug.print("\n栈是否为空 = {}", .{is_empty});

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}

4 changes: 2 additions & 2 deletions codes/zig/chapter_stack_and_queue/queue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub fn main() !void {
std.debug.print("\n队列长度 size = {}", .{size});

// 判断队列是否为空
var empty = if (queue.len == 0) true else false;
std.debug.print("\n队列是否为空 = {}", .{empty});
var is_empty = if (queue.len == 0) true else false;
std.debug.print("\n队列是否为空 = {}", .{is_empty});

_ = try std.io.getStdIn().reader().readByte();
}
3 changes: 1 addition & 2 deletions codes/zig/chapter_stack_and_queue/stack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ pub fn main() !void {
var empty = if (stack.items.len == 0) true else false;
std.debug.print("\n栈是否为空 = {}", .{empty});

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}
3 changes: 1 addition & 2 deletions codes/zig/chapter_tree/binary_tree.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub fn main() !void {
std.debug.print("删除结点 P 后\n", .{});
try inc.PrintUtil.printTree(&n1, null, false);

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}

3 changes: 1 addition & 2 deletions codes/zig/chapter_tree/binary_tree_bfs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,5 @@ pub fn main() !void {
std.debug.print("\n层序遍历的结点打印序列 = ", .{});
inc.PrintUtil.printList(i32, list);

const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
_ = try std.io.getStdIn().reader().readByte();
}
1 change: 0 additions & 1 deletion codes/zig/include/ListNode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
const std = @import("std");

// Definition for a singly-linked list node
// 编译期泛型
pub fn ListNode(comptime T: type) type {
return struct {
const Self = @This();
Expand Down
2 changes: 1 addition & 1 deletion codes/zig/include/PrintUtil.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn printQueue(comptime T: type, queue: std.TailQueue(T)) void {
}
}

// Print a HashMap
// Print a hash map
pub fn printHashMap(comptime TKey: type, comptime TValue: type, map: std.AutoHashMap(TKey, TValue)) void {
var it = map.iterator();
while (it.next()) |kv| {
Expand Down
1 change: 0 additions & 1 deletion codes/zig/include/TreeNode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
const std = @import("std");

// Definition for a binary tree node
// 编译期泛型
pub fn TreeNode(comptime T: type) type {
return struct {
const Self = @This();
Expand Down

0 comments on commit 0635010

Please sign in to comment.