Files
hello-algo/zh-hant/codes/zig/include/PrintUtil.zig
Yudong Jin 44effb07e6 Bug fixes and improvements (#1813)
* Sync zh and zh-hant version.

* Add the Warp sponsor banner.

* Update README with acknowledgments and Warp recommendation

Added acknowledgments and a recommendation for the Warp terminal application.

* Update README.md

* Update links in README.md to use HTTPS

* Sync zh and zh-hant versions.

* Add special thanks for Warp spnsorship.

* Use official warp image link.
2025-09-23 20:44:38 +08:00

43 lines
1.4 KiB
Zig

// File: PrintUtil.zig
// Created Time: 2023-01-07
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
pub const ListUtil = @import("ListNode.zig");
pub const ListNode = ListUtil.ListNode;
pub const TreeUtil = @import("TreeNode.zig");
pub const TreeNode = TreeUtil.TreeNode;
// 列印佇列
pub fn printQueue(comptime T: type, queue: std.TailQueue(T)) void {
var node = queue.first;
std.debug.print("[", .{});
var i: i32 = 0;
while (node != null) : (i += 1) {
var data = node.?.data;
std.debug.print("{}{s}", .{ data, if (i == queue.len - 1) "]" else ", " });
node = node.?.next;
}
}
// 列印雜湊表
pub fn printHashMap(comptime TKey: type, comptime TValue: type, map: std.AutoHashMap(TKey, TValue)) void {
var it = map.iterator();
while (it.next()) |kv| {
var key = kv.key_ptr.*;
var value = kv.value_ptr.*;
std.debug.print("{} -> {s}\n", .{ key, value });
}
}
// 列印堆積
pub fn printHeap(comptime T: type, mem_allocator: std.mem.Allocator, queue: anytype) !void {
var arr = queue.items;
var len = queue.len;
std.debug.print("堆積的陣列表示:", .{});
printArray(T, arr[0..len]);
std.debug.print("\n堆積的樹狀表示:\n", .{});
var root = try TreeUtil.arrToTree(T, mem_allocator, arr[0..len]);
try printTree(root, null, false);
}