Skip to content

Commit 6d36e3e

Browse files
committed
update
1 parent 6afa869 commit 6d36e3e

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

zig/chapter_heap/heap.zig

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,45 +40,45 @@ pub fn main() !void {
4040
// 初始化堆
4141
// 初始化小顶堆
4242
const PQlt = std.PriorityQueue(i32, void, lessThan);
43-
var minHeap = PQlt.init(std.heap.page_allocator, {});
44-
defer minHeap.deinit();
43+
var min_heap = PQlt.init(std.heap.page_allocator, {});
44+
defer min_heap.deinit();
4545
// 初始化大顶堆
4646
const PQgt = std.PriorityQueue(i32, void, greaterThan);
47-
var maxHeap = PQgt.init(std.heap.page_allocator, {});
48-
defer maxHeap.deinit();
47+
var max_heap = PQgt.init(std.heap.page_allocator, {});
48+
defer max_heap.deinit();
4949

5050
std.debug.print("\n以下测试样例为大顶堆", .{});
5151

5252
// 元素入堆
53-
try testPush(i32, mem_allocator, &maxHeap, 1);
54-
try testPush(i32, mem_allocator, &maxHeap, 3);
55-
try testPush(i32, mem_allocator, &maxHeap, 2);
56-
try testPush(i32, mem_allocator, &maxHeap, 5);
57-
try testPush(i32, mem_allocator, &maxHeap, 4);
53+
try testPush(i32, mem_allocator, &max_heap, 1);
54+
try testPush(i32, mem_allocator, &max_heap, 3);
55+
try testPush(i32, mem_allocator, &max_heap, 2);
56+
try testPush(i32, mem_allocator, &max_heap, 5);
57+
try testPush(i32, mem_allocator, &max_heap, 4);
5858

5959
// 获取堆顶元素
60-
var peek = maxHeap.peek().?;
60+
var peek = max_heap.peek().?;
6161
std.debug.print("\n堆顶元素为 {}\n", .{peek});
6262

6363
// 堆顶元素出堆
64-
try testPop(i32, mem_allocator, &maxHeap);
65-
try testPop(i32, mem_allocator, &maxHeap);
66-
try testPop(i32, mem_allocator, &maxHeap);
67-
try testPop(i32, mem_allocator, &maxHeap);
68-
try testPop(i32, mem_allocator, &maxHeap);
64+
try testPop(i32, mem_allocator, &max_heap);
65+
try testPop(i32, mem_allocator, &max_heap);
66+
try testPop(i32, mem_allocator, &max_heap);
67+
try testPop(i32, mem_allocator, &max_heap);
68+
try testPop(i32, mem_allocator, &max_heap);
6969

7070
// 获取堆的大小
71-
var size = maxHeap.len;
71+
var size = max_heap.len;
7272
std.debug.print("\n堆元素数量为 {}\n", .{size});
7373

7474
// 判断堆是否为空
75-
var is_empty = if (maxHeap.len == 0) true else false;
75+
var is_empty = if (max_heap.len == 0) true else false;
7676
std.debug.print("\n堆是否为空 {}\n", .{is_empty});
7777

7878
// 输入列表并建堆
79-
try minHeap.addSlice(&[_]i32{ 1, 3, 2, 5, 4 });
79+
try min_heap.addSlice(&[_]i32{ 1, 3, 2, 5, 4 });
8080
std.debug.print("\n输入列表并建立小顶堆后\n", .{});
81-
try inc.PrintUtil.printHeap(i32, mem_allocator, minHeap);
81+
try inc.PrintUtil.printHeap(i32, mem_allocator, min_heap);
8282

8383
_ = try std.io.getStdIn().reader().readByte();
8484
}

zig/chapter_heap/my_heap.zig

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,33 +159,33 @@ pub fn main() !void {
159159
const mem_allocator = mem_arena.allocator();
160160

161161
// 初始化大顶堆
162-
var maxHeap = MaxHeap(i32){};
163-
try maxHeap.init(std.heap.page_allocator, &[_]i32{ 9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2 });
164-
defer maxHeap.deinit();
162+
var max_heap = MaxHeap(i32){};
163+
try max_heap.init(std.heap.page_allocator, &[_]i32{ 9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2 });
164+
defer max_heap.deinit();
165165
std.debug.print("\n输入列表并建堆后\n", .{});
166-
try maxHeap.print(mem_allocator);
166+
try max_heap.print(mem_allocator);
167167

168168
// 获取堆顶元素
169-
var peek = maxHeap.peek();
169+
var peek = max_heap.peek();
170170
std.debug.print("\n堆顶元素为 {}\n", .{peek});
171171

172172
// 元素入堆
173173
const val = 7;
174-
try maxHeap.push(val);
174+
try max_heap.push(val);
175175
std.debug.print("\n元素 {} 入堆后\n", .{val});
176-
try maxHeap.print(mem_allocator);
176+
try max_heap.print(mem_allocator);
177177

178178
// 堆顶元素出堆
179-
peek = try maxHeap.pop();
179+
peek = try max_heap.pop();
180180
std.debug.print("\n堆顶元素 {} 出堆后\n", .{peek});
181-
try maxHeap.print(mem_allocator);
181+
try max_heap.print(mem_allocator);
182182

183183
// 获取堆的大小
184-
var size = maxHeap.size();
184+
var size = max_heap.size();
185185
std.debug.print("\n堆元素数量为 {}", .{size});
186186

187187
// 判断堆是否为空
188-
var is_empty = maxHeap.isEmpty();
188+
var is_empty = max_heap.isEmpty();
189189
std.debug.print("\n堆是否为空 {}\n", .{is_empty});
190190

191191
_ = try std.io.getStdIn().reader().readByte();

0 commit comments

Comments
 (0)