Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Jan 30, 2023
2 parents 9c2caad + ddd5562 commit 9973822
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 98 deletions.
8 changes: 4 additions & 4 deletions codes/c/chapter_array_and_linkedlist/my_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ int capacity(myList *list) {

/* 访问元素 */
int get(myList *list, int index) {
assert(index < list->size);
assert(index >= 0 && index < list->size);
return list->nums[index];
}

/* 更新元素 */
void set(myList *list, int index, int num) {
assert(index < list->size);
assert(index >= 0 && index < list->size);
list->nums[index] = num;
}

Expand All @@ -68,7 +68,7 @@ void add(myList *list, int num) {

/* 中间插入元素 */
void insert(myList *list, int index, int num) {
assert(index < size(list));
assert(index >= 0 && index < size(list));
for (int i = size(list); i > index; --i) {
list->nums[i] = list->nums[i - 1];
}
Expand All @@ -80,7 +80,7 @@ void insert(myList *list, int index, int num) {
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888
int removeNum(myList *list, int index) {
assert(index < size(list));
assert(index >= 0 && index < size(list));
int num = list->nums[index];
for (int i = index; i < size(list) - 1; i++) {
list->nums[i] = list->nums[i + 1];
Expand Down
8 changes: 4 additions & 4 deletions codes/cpp/chapter_array_and_linkedlist/my_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ class MyList {
/* 访问元素 */
int get(int index) {
// 索引如果越界则抛出异常,下同
if (index >= size())
if (index < 0 || index >= size())
throw out_of_range("索引越界");
return nums[index];
}

/* 更新元素 */
void set(int index, int num) {
if (index >= size())
if (index < 0 || index >= size())
throw out_of_range("索引越界");
nums[index] = num;
}
Expand All @@ -62,7 +62,7 @@ class MyList {

/* 中间插入元素 */
void insert(int index, int num) {
if (index >= size())
if (index < 0 || index >= size())
throw out_of_range("索引越界");
// 元素数量超出容量时,触发扩容机制
if (size() == capacity())
Expand All @@ -78,7 +78,7 @@ class MyList {

/* 删除元素 */
int remove(int index) {
if (index >= size())
if (index < 0 || index >= size())
throw out_of_range("索引越界");
int num = nums[index];
// 索引 i 之后的元素都向前移动一位
Expand Down
8 changes: 4 additions & 4 deletions codes/csharp/chapter_array_and_linkedlist/my_list.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public int Capacity()
public int Get(int index)
{
// 索引如果越界则抛出异常,下同
if (index >= size)
if (index < 0 || index >= size)
throw new IndexOutOfRangeException("索引越界");
return nums[index];
}

/* 更新元素 */
public void Set(int index, int num)
{
if (index >= size)
if (index < 0 || index >= size)
throw new IndexOutOfRangeException("索引越界");
nums[index] = num;
}
Expand All @@ -64,7 +64,7 @@ public void Add(int num)
/* 中间插入元素 */
public void Insert(int index, int num)
{
if (index >= size)
if (index < 0 || index >= size)
throw new IndexOutOfRangeException("索引越界");
// 元素数量超出容量时,触发扩容机制
if (size == Capacity())
Expand All @@ -82,7 +82,7 @@ public void Insert(int index, int num)
/* 删除元素 */
public int Remove(int index)
{
if (index >= size)
if (index < 0 || index >= size)
throw new IndexOutOfRangeException("索引越界");
int num = nums[index];
// 将索引 index 之后的元素都向前移动一位
Expand Down
8 changes: 4 additions & 4 deletions codes/go/chapter_array_and_linkedlist/my_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func (l *myList) capacity() int {
/* 访问元素 */
func (l *myList) get(index int) int {
// 索引如果越界则抛出异常,下同
if index >= l.numsSize {
if index < 0 || index >= l.numsSize {
panic("索引越界")
}
return l.nums[index]
}

/* 更新元素 */
func (l *myList) set(num, index int) {
if index >= l.numsSize {
if index < 0 || index >= l.numsSize {
panic("索引越界")
}
l.nums[index] = num
Expand All @@ -62,7 +62,7 @@ func (l *myList) add(num int) {

/* 中间插入元素 */
func (l *myList) insert(num, index int) {
if index >= l.numsSize {
if index < 0 || index >= l.numsSize {
panic("索引越界")
}
// 元素数量超出容量时,触发扩容机制
Expand All @@ -80,7 +80,7 @@ func (l *myList) insert(num, index int) {

/* 删除元素 */
func (l *myList) remove(index int) int {
if index >= l.numsSize {
if index < 0 || index >= l.numsSize {
panic("索引越界")
}
num := l.nums[index]
Expand Down
8 changes: 4 additions & 4 deletions codes/java/chapter_array_and_linkedlist/my_list.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public int capacity() {
/* 访问元素 */
public int get(int index) {
// 索引如果越界则抛出异常,下同
if (index >= size)
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("索引越界");
return nums[index];
}

/* 更新元素 */
public void set(int index, int num) {
if (index >= size)
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("索引越界");
nums[index] = num;
}
Expand All @@ -57,7 +57,7 @@ public void add(int num) {

/* 中间插入元素 */
public void insert(int index, int num) {
if (index >= size)
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("索引越界");
// 元素数量超出容量时,触发扩容机制
if (size == capacity())
Expand All @@ -73,7 +73,7 @@ public void insert(int index, int num) {

/* 删除元素 */
public int remove(int index) {
if (index >= size)
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("索引越界");
int num = nums[index];
// 将索引 index 之后的元素都向前移动一位
Expand Down
12 changes: 6 additions & 6 deletions codes/javascript/chapter_array_and_linkedlist/my_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class MyList {
/* 访问元素 */
get(index) {
// 索引如果越界则抛出异常,下同
if (index >= this.#size) {
if (index < 0 || index >= this.#size)
throw new Error('索引越界');
}
return this.#nums[index];
}

/* 更新元素 */
set(index, num) {
if (index >= this._size) throw new Error('索引越界');
if (index < 0 || index >= this.#size)
throw new Error('索引越界');
this.#nums[index] = num;
}

Expand All @@ -54,9 +54,8 @@ class MyList {

/* 中间插入元素 */
insert(index, num) {
if (index >= this.#size) {
if (index < 0 || index >= this.#size)
throw new Error('索引越界');
}
// 元素数量超出容量时,触发扩容机制
if (this.#size === this.#capacity) {
this.extendCapacity();
Expand All @@ -72,7 +71,8 @@ class MyList {

/* 删除元素 */
remove(index) {
if (index >= this.#size) throw new Error('索引越界');
if (index < 0 || index >= this.#size)
throw new Error('索引越界');
let num = this.#nums[index];
// 将索引 index 之后的元素都向前移动一位
for (let j = index; j < this.#size - 1; j++) {
Expand Down
8 changes: 4 additions & 4 deletions codes/python/chapter_array_and_linkedlist/my_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ def capacity(self):
""" 访问元素 """
def get(self, index):
# 索引如果越界则抛出异常,下同
assert index < self.__size, "索引越界"
assert index >= 0 and index < self.__size, "索引越界"
return self.__nums[index]

""" 更新元素 """
def set(self, num, index):
assert index < self.__size, "索引越界"
assert index >= 0 and index < self.__size, "索引越界"
self.__nums[index] = num

""" 中间插入(尾部添加)元素 """
def add(self, num, index=-1):
assert index < self.__size, "索引越界"
assert index >= 0 and index < self.__size, "索引越界"
# 若不指定索引 index ,则向数组尾部添加元素
if index == -1:
index = self.__size
Expand All @@ -54,7 +54,7 @@ def add(self, num, index=-1):

""" 删除元素 """
def remove(self, index):
assert index < self.__size, "索引越界"
assert index >= 0 and index < self.__size, "索引越界"
num = self.nums[index]
# 索引 i 之后的元素都向前移动一位
for j in range(index, self.__size - 1):
Expand Down
8 changes: 4 additions & 4 deletions codes/swift/chapter_array_and_linkedlist/my_list.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class MyList {
/* 访问元素 */
func get(index: Int) -> Int {
// 索引如果越界则抛出错误,下同
if index >= _size {
if index < 0 || index >= _size {
fatalError("索引越界")
}
return nums[index]
}

/* 更新元素 */
func set(index: Int, num: Int) {
if index >= _size {
if index < 0 || index >= _size {
fatalError("索引越界")
}
nums[index] = num
Expand All @@ -56,7 +56,7 @@ class MyList {

/* 中间插入元素 */
func insert(index: Int, num: Int) {
if index >= _size {
if index < 0 || index >= _size {
fatalError("索引越界")
}
// 元素数量超出容量时,触发扩容机制
Expand All @@ -75,7 +75,7 @@ class MyList {
/* 删除元素 */
@discardableResult
func remove(index: Int) -> Int {
if index >= _size {
if index < 0 || index >= _size {
fatalError("索引越界")
}
let num = nums[index]
Expand Down
15 changes: 7 additions & 8 deletions codes/typescript/chapter_array_and_linkedlist/my_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,32 @@ class MyList {
/* 访问元素 */
public get(index: number): number {
// 索引如果越界则抛出异常,下同
if (index >= this._size) {
if (index < 0 || index >= this._size)
throw new Error('索引越界');
}
return this.nums[index];
}

/* 更新元素 */
public set(index: number, num: number): void {
if (index >= this._size) throw new Error('索引越界');
if (index < 0 || index >= this._size)
throw new Error('索引越界');
this.nums[index] = num;
}

/* 尾部添加元素 */
public add(num: number): void {
// 如果长度等于容量,则需要扩容
if (this._size === this._capacity) {
if (this._size === this._capacity)
this.extendCapacity();
}
// 将新元素添加到列表尾部
this.nums[this._size] = num;
this._size++;
}

/* 中间插入元素 */
public insert(index: number, num: number): void {
if (index >= this._size) {
if (index < 0 || index >= this._size)
throw new Error('索引越界');
}
// 元素数量超出容量时,触发扩容机制
if (this._size === this._capacity) {
this.extendCapacity();
Expand All @@ -72,7 +70,8 @@ class MyList {

/* 删除元素 */
public remove(index: number): number {
if (index >= this._size) throw new Error('索引越界');
if (index < 0 || index >= this._size)
throw new Error('索引越界');
let num = this.nums[index];
// 将索引 index 之后的元素都向前移动一位
for (let j = index; j < this._size - 1; j++) {
Expand Down
18 changes: 9 additions & 9 deletions codes/zig/chapter_array_and_linkedlist/my_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ pub fn MyList(comptime T: type) type {
return struct {
const Self = @This();

nums: []T = undefined, // 数组(存储列表元素)
numsCapacity: usize = 10, // 列表容量
numSize: usize = 0, // 列表长度(即当前元素数量)
extendRatio: usize = 2, // 每次列表扩容的倍数
nums: []T = undefined, // 数组(存储列表元素)
numsCapacity: usize = 10, // 列表容量
numSize: usize = 0, // 列表长度(即当前元素数量)
extendRatio: usize = 2, // 每次列表扩容的倍数
mem_arena: ?std.heap.ArenaAllocator = null,
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
mem_allocator: std.mem.Allocator = undefined, // 内存分配器

// 构造函数(分配内存+初始化列表)
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
Expand Down Expand Up @@ -47,14 +47,14 @@ pub fn MyList(comptime T: type) type {
// 访问元素
pub fn get(self: *Self, index: usize) T {
// 索引如果越界则抛出异常,下同
if (index >= self.size()) @panic("索引越界");
if (index < 0 || index >= self.size()) @panic("索引越界");
return self.nums[index];
}

// 更新元素
pub fn set(self: *Self, index: usize, num: T) void {
// 索引如果越界则抛出异常,下同
if (index >= self.size()) @panic("索引越界");
if (index < 0 || index >= self.size()) @panic("索引越界");
self.nums[index] = num;
}

Expand All @@ -69,7 +69,7 @@ pub fn MyList(comptime T: type) type {

// 中间插入元素
pub fn insert(self: *Self, index: usize, num: T) !void {
if (index >= self.size()) @panic("索引越界");
if (index < 0 || index >= self.size()) @panic("索引越界");
// 元素数量超出容量时,触发扩容机制
if (self.size() == self.capacity()) try self.extendCapacity();
// 索引 i 以及之后的元素都向后移动一位
Expand All @@ -84,7 +84,7 @@ pub fn MyList(comptime T: type) type {

// 删除元素
pub fn remove(self: *Self, index: usize) T {
if (index >= self.size()) @panic("索引越界");
if (index < 0 || index >= self.size()) @panic("索引越界");
var num = self.nums[index];
// 索引 i 之后的元素都向前移动一位
var j = index;
Expand Down
Loading

0 comments on commit 9973822

Please sign in to comment.