Unify the function naming of

queue from `offer()` to `push()`
This commit is contained in:
Yudong Jin
2023-02-02 01:43:01 +08:00
parent a0ee691475
commit 7d14c9440e
29 changed files with 184 additions and 189 deletions

View File

@@ -50,7 +50,7 @@ pub fn ArrayQueue(comptime T: type) type {
}
// 入队
pub fn offer(self: *Self, num: T) !void {
pub fn push(self: *Self, num: T) !void {
if (self.size() == self.capacity()) {
std.debug.print("队列已满\n", .{});
return;
@@ -102,11 +102,11 @@ pub fn main() !void {
defer queue.deinit();
// 元素入队
try queue.offer(1);
try queue.offer(3);
try queue.offer(2);
try queue.offer(5);
try queue.offer(4);
try queue.push(1);
try queue.push(3);
try queue.push(2);
try queue.push(5);
try queue.push(4);
std.debug.print("队列 queue = ", .{});
inc.PrintUtil.printArray(i32, try queue.toArray());
@@ -130,7 +130,7 @@ pub fn main() !void {
// 测试环形数组
var i: i32 = 0;
while (i < 10) : (i += 1) {
try queue.offer(i);
try queue.push(i);
_ = queue.poll();
std.debug.print("\n第 {} 轮入队 + 出队后 queue = ", .{i});
inc.PrintUtil.printArray(i32, try queue.toArray());

View File

@@ -62,7 +62,7 @@ pub fn LinkedListDeque(comptime T: type) type {
}
// 入队操作
pub fn offer(self: *Self, num: T, isFront: bool) !void {
pub fn push(self: *Self, num: T, isFront: bool) !void {
var node = try self.mem_allocator.create(ListNode(T));
node.init(num);
// 若链表为空,则令 front, rear 都指向 node
@@ -86,13 +86,13 @@ pub fn LinkedListDeque(comptime T: type) type {
}
// 队首入队
pub fn offerFirst(self: *Self, num: T) !void {
try self.offer(num, true);
pub fn pushFirst(self: *Self, num: T) !void {
try self.push(num, true);
}
// 队尾入队
pub fn offerLast(self: *Self, num: T) !void {
try self.offer(num, false);
pub fn pushLast(self: *Self, num: T) !void {
try self.push(num, false);
}
// 出队操作
@@ -187,7 +187,7 @@ pub fn main() !void {
// 队尾入队
for (nums) |num| {
try deque.offerLast(num);
try deque.pushLast(num);
std.debug.print("\n元素 {} 队尾入队后,队列为\n", .{num});
try deque.print();
}
@@ -203,7 +203,7 @@ pub fn main() !void {
// 队首入队
for (nums) |num| {
try deque.offerFirst(num);
try deque.pushFirst(num);
std.debug.print("\n元素 {} 队首入队后,队列为\n", .{num});
try deque.print();
}

View File

@@ -50,7 +50,7 @@ pub fn LinkedListQueue(comptime T: type) type {
}
// 入队
pub fn offer(self: *Self, num: T) !void {
pub fn push(self: *Self, num: T) !void {
// 尾结点后添加 num
var node = try self.mem_allocator.create(inc.ListNode(T));
node.init(num);
@@ -98,11 +98,11 @@ pub fn main() !void {
defer queue.deinit();
// 元素入队
try queue.offer(1);
try queue.offer(3);
try queue.offer(2);
try queue.offer(5);
try queue.offer(4);
try queue.push(1);
try queue.push(3);
try queue.push(2);
try queue.push(5);
try queue.push(4);
std.debug.print("队列 queue = ", .{});
inc.PrintUtil.printArray(i32, try queue.toArray());