Skip to content

Commit

Permalink
Fix my list.
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Dec 25, 2022
1 parent 8733557 commit 01b6c8b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions codes/go/chapter_array_and_linkedlist/my_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,19 @@ func (l *MyList) insert(num, index int) {
}

/* 删除元素 */
func (l *MyList) remove(index int) {
func (l *MyList) remove(index int) int {
if index >= l.numsSize {
panic("索引越界")
}
num := l.nums[index]
// 索引 i 之后的元素都向前移动一位
for j := index; j < l.numsSize-1; j++ {
l.nums[j] = l.nums[j+1]
}
// 更新元素数量
l.numsSize--
// 返回被删除元素
return num
}

/* 列表扩容 */
Expand All @@ -103,4 +106,4 @@ func (l *MyList) extendCapacity() {
func (l *MyList) toArray() []int {
// 仅转换有效长度范围内的列表元素
return l.nums[:l.numsSize]
}
}
3 changes: 3 additions & 0 deletions codes/python/chapter_array_and_linkedlist/my_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ def add(self, num, index=-1):
""" 删除元素 """
def remove(self, index):
assert index < self.__size, "索引越界"
num = self.nums[index]
# 索引 i 之后的元素都向前移动一位
for j in range(index, self.__size - 1):
self.__nums[j] = self.__nums[j + 1]
# 更新元素数量
self.__size -= 1
# 返回被删除元素
return num

""" 列表扩容 """
def extend_capacity(self):
Expand Down

0 comments on commit 01b6c8b

Please sign in to comment.