mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-03 02:54:22 +00:00
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:
@@ -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]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user