add zig codes for Section Queue and Deque (#291)

* add zig codes for Section 'Queue'

* update zig codes for Section 'Queue'

* update zig codes for Section 'Queue'

* update zig codes for Section 'Queue'

* update zig codes for Section 'Queue' and 'Deque'

* update zig codes for Section 'Queue' and 'Deque'

* update zig codes for Section 'Queue' and 'Deque'
This commit is contained in:
sjinzh 2023-01-21 01:22:13 +08:00 committed by GitHub
parent 0b5761eaf5
commit d9f8c53e4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 670 additions and 0 deletions

View file

@ -160,6 +160,73 @@ pub fn build(b: *std.build.Builder) void {
const run_step_array_stack = b.step("run_array_stack", "Run array_stack");
run_step_array_stack.dependOn(&run_cmd_array_stack.step);
// Section: "Queue"
// Source File: "chapter_stack_and_queue/queue.zig"
// Run Command: zig build run_queue
const exe_queue = b.addExecutable("queue", "chapter_stack_and_queue/queue.zig");
exe_queue.addPackagePath("include", "include/include.zig");
exe_queue.setTarget(target);
exe_queue.setBuildMode(mode);
exe_queue.install();
const run_cmd_queue = exe_queue.run();
run_cmd_queue.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd_queue.addArgs(args);
const run_step_queue = b.step("run_queue", "Run queue");
run_step_queue.dependOn(&run_cmd_queue.step);
// Source File: "chapter_stack_and_queue/array_queue.zig"
// Run Command: zig build run_array_queue
const exe_array_queue = b.addExecutable("array_queue", "chapter_stack_and_queue/array_queue.zig");
exe_array_queue.addPackagePath("include", "include/include.zig");
exe_array_queue.setTarget(target);
exe_array_queue.setBuildMode(mode);
exe_array_queue.install();
const run_cmd_array_queue = exe_array_queue.run();
run_cmd_array_queue.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd_array_queue.addArgs(args);
const run_step_array_queue = b.step("run_array_queue", "Run array_queue");
run_step_array_queue.dependOn(&run_cmd_array_queue.step);
// Source File: "chapter_stack_and_queue/linkedlist_queue.zig"
// Run Command: zig build run_linkedlist_queue
const exe_linkedlist_queue = b.addExecutable("linkedlist_queue", "chapter_stack_and_queue/linkedlist_queue.zig");
exe_linkedlist_queue.addPackagePath("include", "include/include.zig");
exe_linkedlist_queue.setTarget(target);
exe_linkedlist_queue.setBuildMode(mode);
exe_linkedlist_queue.install();
const run_cmd_linkedlist_queue = exe_linkedlist_queue.run();
run_cmd_linkedlist_queue.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd_linkedlist_queue.addArgs(args);
const run_step_linkedlist_queue = b.step("run_linkedlist_queue", "Run linkedlist_queue");
run_step_linkedlist_queue.dependOn(&run_cmd_linkedlist_queue.step);
// Section: "Deque"
// Source File: "chapter_stack_and_queue/deque.zig"
// Run Command: zig build run_deque
const exe_deque = b.addExecutable("deque", "chapter_stack_and_queue/deque.zig");
exe_deque.addPackagePath("include", "include/include.zig");
exe_deque.setTarget(target);
exe_deque.setBuildMode(mode);
exe_deque.install();
const run_cmd_deque = exe_deque.run();
run_cmd_deque.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd_deque.addArgs(args);
const run_step_deque = b.step("run_deque", "Run deque");
run_step_deque.dependOn(&run_cmd_deque.step);
// Source File: "chapter_stack_and_queue/linkedlist_deque.zig"
// Run Command: zig build run_linkedlist_deque
const exe_linkedlist_deque = b.addExecutable("linkedlist_deque", "chapter_stack_and_queue/linkedlist_deque.zig");
exe_linkedlist_deque.addPackagePath("include", "include/include.zig");
exe_linkedlist_deque.setTarget(target);
exe_linkedlist_deque.setBuildMode(mode);
exe_linkedlist_deque.install();
const run_cmd_linkedlist_deque = exe_linkedlist_deque.run();
run_cmd_linkedlist_deque.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd_linkedlist_deque.addArgs(args);
const run_step_linkedlist_deque = b.step("run_linkedlist_deque", "Run linkedlist_deque");
run_step_linkedlist_deque.dependOn(&run_cmd_linkedlist_deque.step);
// Section: "Hash Map"
// Source File: "chapter_hashing/hash_map.zig"
// Run Command: zig build run_hash_map

View file

@ -0,0 +1,138 @@
// File: array_queue.zig
// Created Time: 2023-01-15
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
//
pub fn ArrayQueue(comptime T: type) type {
return struct {
const Self = @This();
nums: []T = undefined, //
cap: usize = 0, //
front: usize = 0, //
rear: usize = 0, // + 1
mem_arena: ?std.heap.ArenaAllocator = null,
mem_allocator: std.mem.Allocator = undefined, //
// +
pub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {
if (self.mem_arena == null) {
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
self.mem_allocator = self.mem_arena.?.allocator();
}
self.cap = cap;
self.nums = try self.mem_allocator.alloc(T, self.cap);
std.mem.set(T, self.nums, @as(T, 0));
}
//
pub fn deinit(self: *Self) void {
if (self.mem_arena == null) return;
self.mem_arena.?.deinit();
}
//
pub fn capacity(self: *Self) usize {
return self.cap;
}
//
pub fn size(self: *Self) usize {
// rear < front
return (self.capacity() + self.rear - self.front) % self.capacity();
}
//
pub fn isEmpty(self: *Self) bool {
return self.rear == self.front;
}
//
pub fn offer(self: *Self, num: T) !void {
if (self.size() == self.capacity()) {
std.debug.print("队列已满\n", .{});
return;
}
// num
self.nums[self.rear] = num;
//
self.rear = (self.rear + 1) % self.capacity();
}
//
pub fn poll(self: *Self) T {
var num = self.peek();
//
self.front = (self.front + 1) % self.capacity();
return num;
}
// 访
pub fn peek(self: *Self) T {
if (self.isEmpty()) @panic("队列为空");
return self.nums[self.front];
}
//
pub fn toArray(self: *Self) ![]T {
//
var res = try self.mem_allocator.alloc(T, self.size());
std.mem.set(T, res, @as(T, 0));
var i: usize = 0;
var j: usize = self.front;
while (i < self.size()) : ({ i += 1; j += 1; }) {
res[i] = self.nums[j % self.capacity()];
}
return res;
}
};
}
// Driver Code
pub fn main() !void {
//
var capacity: usize = 10;
var queue = ArrayQueue(i32){};
try queue.init(std.heap.page_allocator, capacity);
defer queue.deinit();
//
try queue.offer(1);
try queue.offer(3);
try queue.offer(2);
try queue.offer(5);
try queue.offer(4);
std.debug.print("队列 queue = ", .{});
inc.PrintUtil.printArray(i32, try queue.toArray());
// 访
var peek = queue.peek();
std.debug.print("\n队首元素 peek = {}", .{peek});
//
var poll = queue.poll();
std.debug.print("\n出队元素 poll = {},出队后 queue = ", .{poll});
inc.PrintUtil.printArray(i32, try queue.toArray());
//
var size = queue.size();
std.debug.print("\n队列长度 size = {}", .{size});
//
var isEmpty = queue.isEmpty();
std.debug.print("\n队列是否为空 = {}", .{isEmpty});
//
var i: i32 = 0;
while (i < 10) : (i += 1) {
try queue.offer(i);
_ = queue.poll();
std.debug.print("\n第 {} 轮入队 + 出队后 queue = ", .{i});
inc.PrintUtil.printArray(i32, try queue.toArray());
}
_ = try std.io.getStdIn().reader().readByte();
}

View file

@ -0,0 +1,51 @@
// File: deque.zig
// Created Time: 2023-01-15
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
// Driver Code
pub fn main() !void {
//
const L = std.TailQueue(i32);
var deque = L{};
//
var node1 = L.Node{ .data = 2 };
var node2 = L.Node{ .data = 5 };
var node3 = L.Node{ .data = 4 };
var node4 = L.Node{ .data = 3 };
var node5 = L.Node{ .data = 1 };
deque.append(&node1);
deque.append(&node2);
deque.append(&node3);
deque.prepend(&node4);
deque.prepend(&node5);
std.debug.print("双向队列 deque = ", .{});
inc.PrintUtil.printQueue(i32, deque);
// 访
var peekFirst = deque.first.?.data;
std.debug.print("\n队首元素 peekFirst = {}", .{peekFirst});
var peekLast = deque.last.?.data;
std.debug.print("\n队首元素 peekLast = {}", .{peekLast});
//
var pollFirst = deque.popFirst().?.data;
std.debug.print("\n队首出队元素 pollFirst = {},队首出队后 deque = ", .{pollFirst});
inc.PrintUtil.printQueue(i32, deque);
var pollLast = deque.pop().?.data;
std.debug.print("\n队尾出队元素 pollLast = {},队尾出队后 deque = ", .{pollLast});
inc.PrintUtil.printQueue(i32, deque);
//
var size = deque.len;
std.debug.print("\n双向队列长度 size = {}", .{size});
//
var isEmpty = if (deque.len == 0) true else false;
std.debug.print("\n双向队列是否为空 = {}", .{isEmpty});
_ = try std.io.getStdIn().reader().readByte();
}

View file

@ -0,0 +1,229 @@
// File: linkedlist_deque.zig
// Created Time: 2023-01-15
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
//
pub fn ListNode(comptime T: type) type {
return struct {
const Self = @This();
val: T = undefined, //
next: ?*Self = null, //
prev: ?*Self = null, //
// Initialize a list node with specific value
pub fn init(self: *Self, x: i32) void {
self.val = x;
self.next = null;
self.prev = null;
}
};
}
//
pub fn LinkedListDeque(comptime T: type) type {
return struct {
const Self = @This();
front: ?*ListNode(T) = null, // front
rear: ?*ListNode(T) = null, // rear
deqSize: usize = 0, //
mem_arena: ?std.heap.ArenaAllocator = null,
mem_allocator: std.mem.Allocator = undefined, //
// +
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
if (self.mem_arena == null) {
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
self.mem_allocator = self.mem_arena.?.allocator();
}
self.front = null;
self.rear = null;
self.deqSize = 0;
}
//
pub fn deinit(self: *Self) void {
if (self.mem_arena == null) return;
self.mem_arena.?.deinit();
}
//
pub fn size(self: *Self) usize {
return self.deqSize;
}
//
pub fn isEmpty(self: *Self) bool {
return self.size() == 0;
}
//
pub fn offer(self: *Self, num: T, isFront: bool) !void {
var node = try self.mem_allocator.create(ListNode(T));
node.init(num);
// front, rear node
if (self.isEmpty()) {
self.front = node;
self.rear = node;
//
} else if (isFront) {
// node
self.front.?.prev = node;
node.next = self.front;
self.front = node; //
//
} else {
// node
self.rear.?.next = node;
node.prev = self.rear;
self.rear = node; //
}
self.deqSize += 1; //
}
//
pub fn offerFirst(self: *Self, num: T) !void {
try self.offer(num, true);
}
//
pub fn offerLast(self: *Self, num: T) !void {
try self.offer(num, false);
}
//
pub fn poll(self: *Self, isFront: bool) T {
if (self.isEmpty()) @panic("双向队列为空");
var val: T = undefined;
//
if (isFront) {
val = self.front.?.val; //
//
var fNext = self.front.?.next;
if (fNext != null) {
fNext.?.prev = null;
self.front.?.next = null;
}
self.front = fNext; //
//
} else {
val = self.rear.?.val; //
//
var rPrev = self.rear.?.prev;
if (rPrev != null) {
rPrev.?.next = null;
self.rear.?.prev = null;
}
self.rear = rPrev; //
}
self.deqSize -= 1; //
return val;
}
//
pub fn pollFirst(self: *Self) T {
return self.poll(true);
}
//
pub fn pollLast(self: *Self) T {
return self.poll(false);
}
// 访
pub fn peekFirst(self: *Self) T {
if (self.isEmpty()) @panic("双向队列为空");
return self.front.?.val;
}
// 访
pub fn peekLast(self: *Self) T {
if (self.isEmpty()) @panic("双向队列为空");
return self.rear.?.val;
}
//
pub fn toArray(self: *Self) ![]T {
var node = self.front;
var res = try self.mem_allocator.alloc(T, self.size());
std.mem.set(T, res, @as(T, 0));
var i: usize = 0;
while (i < res.len) : (i += 1) {
res[i] = node.?.val;
node = node.?.next;
}
return res;
}
//
pub fn print(self: *Self) !void {
var nums = try self.toArray();
std.debug.print("[", .{});
if (nums.len > 0) {
for (nums) |num, j| {
std.debug.print("{}{s}", .{num, if (j == nums.len - 1) "]" else " <-> " });
}
} else {
std.debug.print("]", .{});
}
}
};
}
// Driver Code
pub fn main() !void {
//
var deque = LinkedListDeque(i32){};
try deque.init(std.heap.page_allocator);
defer deque.deinit();
std.debug.print("初始化空队列\n", .{});
try deque.print();
var nums = [_]i32{ 1, 2, 3}; //
//
for (nums) |num| {
try deque.offerLast(num);
std.debug.print("\n元素 {} 队尾入队后,队列为\n", .{num});
try deque.print();
}
//
var last = deque.peekLast();
std.debug.print("\n队尾元素为 {}", .{last});
//
while (!deque.isEmpty()) {
last = deque.pollLast();
std.debug.print("\n队尾出队元素为 {} ,队列为\n", .{last});
try deque.print();
}
//
for (nums) |num| {
try deque.offerFirst(num);
std.debug.print("\n元素 {} 队首入队后,队列为\n", .{num});
try deque.print();
}
//
var first = deque.peekFirst();
std.debug.print("\n队首元素为 {}", .{first});
//
while (!deque.isEmpty()) {
first = deque.pollFirst();
std.debug.print("\n队首出队元素为 {} ,队列为\n", .{first});
try deque.print();
}
//
var size = deque.size();
std.debug.print("\n队列长度 size = {}", .{size});
//
var isEmpty = deque.isEmpty();
std.debug.print("\n双向队列是否为空 = {}", .{isEmpty});
_ = try std.io.getStdIn().reader().readByte();
}

View file

@ -0,0 +1,127 @@
// File: linkedlist_queue.zig
// Created Time: 2023-01-15
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
//
pub fn LinkedListQueue(comptime T: type) type {
return struct {
const Self = @This();
front: ?*inc.ListNode(T) = null, // front
rear: ?*inc.ListNode(T) = null, // rear
queSize: usize = 0, //
mem_arena: ?std.heap.ArenaAllocator = null,
mem_allocator: std.mem.Allocator = undefined, //
// +
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
if (self.mem_arena == null) {
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
self.mem_allocator = self.mem_arena.?.allocator();
}
self.front = null;
self.rear = null;
self.queSize = 0;
}
//
pub fn deinit(self: *Self) void {
if (self.mem_arena == null) return;
self.mem_arena.?.deinit();
}
//
pub fn size(self: *Self) usize {
return self.queSize;
}
//
pub fn isEmpty(self: *Self) bool {
return self.size() == 0;
}
// 访
pub fn peek(self: *Self) T {
if (self.size() == 0) @panic("队列为空");
return self.front.?.val;
}
//
pub fn offer(self: *Self, num: T) !void {
// num
var node = try self.mem_allocator.create(inc.ListNode(T));
node.init(num);
//
if (self.front == null) {
self.front = node;
self.rear = node;
//
} else {
self.rear.?.next = node;
self.rear = node;
}
self.queSize += 1;
}
//
pub fn poll(self: *Self) T {
var num = self.peek();
//
self.front = self.front.?.next;
self.queSize -= 1;
return num;
}
//
pub fn toArray(self: *Self) ![]T {
var node = self.front;
var res = try self.mem_allocator.alloc(T, self.size());
std.mem.set(T, res, @as(T, 0));
var i: usize = 0;
while (i < res.len) : (i += 1) {
res[i] = node.?.val;
node = node.?.next;
}
return res;
}
};
}
// Driver Code
pub fn main() !void {
//
var queue = LinkedListQueue(i32){};
try queue.init(std.heap.page_allocator);
defer queue.deinit();
//
try queue.offer(1);
try queue.offer(3);
try queue.offer(2);
try queue.offer(5);
try queue.offer(4);
std.debug.print("队列 queue = ", .{});
inc.PrintUtil.printArray(i32, try queue.toArray());
// 访
var peek = queue.peek();
std.debug.print("\n队首元素 peek = {}", .{peek});
//
var poll = queue.poll();
std.debug.print("\n出队元素 poll = {},出队后 queue = ", .{poll});
inc.PrintUtil.printArray(i32, try queue.toArray());
//
var size = queue.size();
std.debug.print("\n队列长度 size = {}", .{size});
//
var isEmpty = queue.isEmpty();
std.debug.print("\n队列是否为空 = {}", .{isEmpty});
_ = try std.io.getStdIn().reader().readByte();
}

View file

@ -0,0 +1,46 @@
// File: queue.zig
// Created Time: 2023-01-15
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
// Driver Code
pub fn main() !void {
//
const L = std.TailQueue(i32);
var queue = L{};
//
var node1 = L.Node{ .data = 1 };
var node2 = L.Node{ .data = 3 };
var node3 = L.Node{ .data = 2 };
var node4 = L.Node{ .data = 5 };
var node5 = L.Node{ .data = 4 };
queue.append(&node1);
queue.append(&node2);
queue.append(&node3);
queue.append(&node4);
queue.append(&node5);
std.debug.print("队列 queue = ", .{});
inc.PrintUtil.printQueue(i32, queue);
// 访
var front = queue.first.?.data;
std.debug.print("\n队首元素 front = {}", .{front});
//
front = queue.popFirst().?.data;
std.debug.print("\n出队元素 front = {},出队后 queue = ", .{front});
inc.PrintUtil.printQueue(i32, queue);
//
var size = queue.len;
std.debug.print("\n队列长度 size = {}", .{size});
//
var empty = if (queue.len == 0) true else false;
std.debug.print("\n队列是否为空 = {}", .{empty});
_ = try std.io.getStdIn().reader().readByte();
}

View file

@ -48,6 +48,18 @@ pub fn printLinkedList(comptime T: type, node: ?*ListNode(T)) !void {
}
}
// Print a queue or deque
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;
}
}
// Print a HashMap
pub fn printHashMap(comptime TKey: type, comptime TValue: type, map: std.AutoHashMap(TKey, TValue)) void {
var it = map.iterator();