fix(array and linkedlist): fix that the printing in the test function is the same as that in other languages

This commit is contained in:
陈国太
2022-12-29 21:49:46 +08:00
parent 1faad9e187
commit cd9f4fc35d
4 changed files with 54 additions and 97 deletions
@@ -9,10 +9,6 @@ import (
"math/rand"
)
/* 初始化数组 */
var arr = [5]int{}
var nums = []int{1, 3, 2, 5, 4}
/* 随机返回一个数组元素 */
func randomAccess(nums []int) int {
randomIndex := rand.Intn(len(nums))
@@ -33,9 +29,9 @@ func extend(nums []int, enlarge int) []int {
}
/* 在数组的索引 index 处插入元素 num */
func insert(nums []int, size int, num int, index int) {
func insert(nums []int, num int, index int) {
// 把索引 index 以及之后的所有元素向后移动一位
for i := size - 1; i > index; i-- {
for i := len(nums) - 1; i > index; i-- {
nums[i] = nums[i-1]
}
// 将 num 赋给 index 处元素
@@ -43,9 +39,9 @@ func insert(nums []int, size int, num int, index int) {
}
/* 删除索引 index 处元素 */
func remove(nums []int, size int, index int) {
func remove(nums []int, index int) {
// 把索引 index 之后的所有元素向前移动一位
for i := index; i < size-1; i++ {
for i := index; i < len(nums)-1; i++ {
nums[i] = nums[i+1]
}
}