mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 14:26:29 +08:00
parent
ad8859502c
commit
6cd6d5589e
3 changed files with 7 additions and 7 deletions
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
// Zig Version: 0.10.0
|
// Zig Version: 0.10.1
|
||||||
// Build Command: zig build
|
// Build Command: zig build
|
||||||
pub fn build(b: *std.build.Builder) void {
|
pub fn build(b: *std.build.Builder) void {
|
||||||
const target = b.standardTargetOptions(.{});
|
const target = b.standardTargetOptions(.{});
|
||||||
|
|
|
@ -47,14 +47,14 @@ pub fn MyList(comptime T: type) type {
|
||||||
// 访问元素
|
// 访问元素
|
||||||
pub fn get(self: *Self, index: usize) T {
|
pub fn get(self: *Self, index: usize) T {
|
||||||
// 索引如果越界则抛出异常,下同
|
// 索引如果越界则抛出异常,下同
|
||||||
if (index < 0 || index >= self.size()) @panic("索引越界");
|
if (index < 0 or index >= self.size()) @panic("索引越界");
|
||||||
return self.nums[index];
|
return self.nums[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新元素
|
// 更新元素
|
||||||
pub fn set(self: *Self, index: usize, num: T) void {
|
pub fn set(self: *Self, index: usize, num: T) void {
|
||||||
// 索引如果越界则抛出异常,下同
|
// 索引如果越界则抛出异常,下同
|
||||||
if (index < 0 || index >= self.size()) @panic("索引越界");
|
if (index < 0 or index >= self.size()) @panic("索引越界");
|
||||||
self.nums[index] = num;
|
self.nums[index] = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ pub fn MyList(comptime T: type) type {
|
||||||
|
|
||||||
// 中间插入元素
|
// 中间插入元素
|
||||||
pub fn insert(self: *Self, index: usize, num: T) !void {
|
pub fn insert(self: *Self, index: usize, num: T) !void {
|
||||||
if (index < 0 || index >= self.size()) @panic("索引越界");
|
if (index < 0 or index >= self.size()) @panic("索引越界");
|
||||||
// 元素数量超出容量时,触发扩容机制
|
// 元素数量超出容量时,触发扩容机制
|
||||||
if (self.size() == self.capacity()) try self.extendCapacity();
|
if (self.size() == self.capacity()) try self.extendCapacity();
|
||||||
// 索引 i 以及之后的元素都向后移动一位
|
// 索引 i 以及之后的元素都向后移动一位
|
||||||
|
@ -84,7 +84,7 @@ pub fn MyList(comptime T: type) type {
|
||||||
|
|
||||||
// 删除元素
|
// 删除元素
|
||||||
pub fn remove(self: *Self, index: usize) T {
|
pub fn remove(self: *Self, index: usize) T {
|
||||||
if (index < 0 || index >= self.size()) @panic("索引越界");
|
if (index < 0 or index >= self.size()) @panic("索引越界");
|
||||||
var num = self.nums[index];
|
var num = self.nums[index];
|
||||||
// 索引 i 之后的元素都向前移动一位
|
// 索引 i 之后的元素都向前移动一位
|
||||||
var j = index;
|
var j = index;
|
||||||
|
|
|
@ -60,7 +60,7 @@ pub fn ArrayQueue(comptime T: type) type {
|
||||||
var rear = (self.front + self.queSize) % self.capacity();
|
var rear = (self.front + self.queSize) % self.capacity();
|
||||||
// 尾结点后添加 num
|
// 尾结点后添加 num
|
||||||
self.nums[rear] = num;
|
self.nums[rear] = num;
|
||||||
self.queSize++;
|
self.queSize += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 出队
|
// 出队
|
||||||
|
@ -68,7 +68,7 @@ pub fn ArrayQueue(comptime T: type) type {
|
||||||
var num = self.peek();
|
var num = self.peek();
|
||||||
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
||||||
self.front = (self.front + 1) % self.capacity();
|
self.front = (self.front + 1) % self.capacity();
|
||||||
self.queSize--;
|
self.queSize -= 1;
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue