mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 17:09:46 +08:00
Update variable names in list and my_list
This commit is contained in:
@@ -13,55 +13,55 @@ import (
|
||||
/* Driver Code */
|
||||
func TestList(t *testing.T) {
|
||||
/* 初始化列表 */
|
||||
list := []int{1, 3, 2, 5, 4}
|
||||
fmt.Println("列表 list =", list)
|
||||
nums := []int{1, 3, 2, 5, 4}
|
||||
fmt.Println("列表 nums =", nums)
|
||||
|
||||
/* 访问元素 */
|
||||
num := list[1] // 访问索引 1 处的元素
|
||||
num := nums[1] // 访问索引 1 处的元素
|
||||
fmt.Println("访问索引 1 处的元素,得到 num =", num)
|
||||
|
||||
/* 更新元素 */
|
||||
list[1] = 0 // 将索引 1 处的元素更新为 0
|
||||
fmt.Println("将索引 1 处的元素更新为 0 ,得到 list =", list)
|
||||
nums[1] = 0 // 将索引 1 处的元素更新为 0
|
||||
fmt.Println("将索引 1 处的元素更新为 0 ,得到 nums =", nums)
|
||||
|
||||
/* 清空列表 */
|
||||
list = nil
|
||||
fmt.Println("清空列表后 list =", list)
|
||||
nums = nil
|
||||
fmt.Println("清空列表后 nums =", nums)
|
||||
|
||||
/* 尾部添加元素 */
|
||||
list = append(list, 1)
|
||||
list = append(list, 3)
|
||||
list = append(list, 2)
|
||||
list = append(list, 5)
|
||||
list = append(list, 4)
|
||||
fmt.Println("添加元素后 list =", list)
|
||||
nums = append(nums, 1)
|
||||
nums = append(nums, 3)
|
||||
nums = append(nums, 2)
|
||||
nums = append(nums, 5)
|
||||
nums = append(nums, 4)
|
||||
fmt.Println("添加元素后 nums =", nums)
|
||||
|
||||
/* 中间插入元素 */
|
||||
list = append(list[:3], append([]int{6}, list[3:]...)...) // 在索引 3 处插入数字 6
|
||||
fmt.Println("在索引 3 处插入数字 6 ,得到 list =", list)
|
||||
nums = append(nums[:3], append([]int{6}, nums[3:]...)...) // 在索引 3 处插入数字 6
|
||||
fmt.Println("在索引 3 处插入数字 6 ,得到 nums =", nums)
|
||||
|
||||
/* 删除元素 */
|
||||
list = append(list[:3], list[4:]...) // 删除索引 3 处的元素
|
||||
fmt.Println("删除索引 3 处的元素,得到 list =", list)
|
||||
nums = append(nums[:3], nums[4:]...) // 删除索引 3 处的元素
|
||||
fmt.Println("删除索引 3 处的元素,得到 nums =", nums)
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
count := 0
|
||||
for i := 0; i < len(list); i++ {
|
||||
for i := 0; i < len(nums); i++ {
|
||||
count++
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0
|
||||
for range list {
|
||||
for range nums {
|
||||
count++
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
list1 := []int{6, 8, 7, 10, 9}
|
||||
list = append(list, list1...) // 将列表 list1 拼接到 list 之后
|
||||
fmt.Println("将列表 list1 拼接到 list 之后,得到 list =", list)
|
||||
nums1 := []int{6, 8, 7, 10, 9}
|
||||
nums = append(nums, nums1...) // 将列表 nums1 拼接到 nums 之后
|
||||
fmt.Println("将列表 nums1 拼接到 nums 之后,得到 nums =", nums)
|
||||
|
||||
/* 排序列表 */
|
||||
sort.Ints(list) // 排序后,列表元素从小到大排列
|
||||
fmt.Println("排序列表后 list =", list)
|
||||
sort.Ints(nums) // 排序后,列表元素从小到大排列
|
||||
fmt.Println("排序列表后 nums =", nums)
|
||||
}
|
||||
|
||||
@@ -6,90 +6,90 @@ package chapter_array_and_linkedlist
|
||||
|
||||
/* 列表类简易实现 */
|
||||
type myList struct {
|
||||
numsCapacity int
|
||||
nums []int
|
||||
numsSize int
|
||||
extendRatio int
|
||||
arrCapacity int
|
||||
arr []int
|
||||
arrSize int
|
||||
extendRatio int
|
||||
}
|
||||
|
||||
/* 构造函数 */
|
||||
func newMyList() *myList {
|
||||
return &myList{
|
||||
numsCapacity: 10, // 列表容量
|
||||
nums: make([]int, 10), // 数组(存储列表元素)
|
||||
numsSize: 0, // 列表长度(即当前元素数量)
|
||||
extendRatio: 2, // 每次列表扩容的倍数
|
||||
arrCapacity: 10, // 列表容量
|
||||
arr: make([]int, 10), // 数组(存储列表元素)
|
||||
arrSize: 0, // 列表长度(即当前元素数量)
|
||||
extendRatio: 2, // 每次列表扩容的倍数
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量) */
|
||||
func (l *myList) size() int {
|
||||
return l.numsSize
|
||||
return l.arrSize
|
||||
}
|
||||
|
||||
/* 获取列表容量 */
|
||||
func (l *myList) capacity() int {
|
||||
return l.numsCapacity
|
||||
return l.arrCapacity
|
||||
}
|
||||
|
||||
/* 访问元素 */
|
||||
func (l *myList) get(index int) int {
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if index < 0 || index >= l.numsSize {
|
||||
if index < 0 || index >= l.arrSize {
|
||||
panic("索引越界")
|
||||
}
|
||||
return l.nums[index]
|
||||
return l.arr[index]
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
func (l *myList) set(num, index int) {
|
||||
if index < 0 || index >= l.numsSize {
|
||||
if index < 0 || index >= l.arrSize {
|
||||
panic("索引越界")
|
||||
}
|
||||
l.nums[index] = num
|
||||
l.arr[index] = num
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
func (l *myList) add(num int) {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if l.numsSize == l.numsCapacity {
|
||||
if l.arrSize == l.arrCapacity {
|
||||
l.extendCapacity()
|
||||
}
|
||||
l.nums[l.numsSize] = num
|
||||
l.arr[l.arrSize] = num
|
||||
// 更新元素数量
|
||||
l.numsSize++
|
||||
l.arrSize++
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
func (l *myList) insert(num, index int) {
|
||||
if index < 0 || index >= l.numsSize {
|
||||
if index < 0 || index >= l.arrSize {
|
||||
panic("索引越界")
|
||||
}
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if l.numsSize == l.numsCapacity {
|
||||
if l.arrSize == l.arrCapacity {
|
||||
l.extendCapacity()
|
||||
}
|
||||
// 将索引 index 以及之后的元素都向后移动一位
|
||||
for j := l.numsSize - 1; j >= index; j-- {
|
||||
l.nums[j+1] = l.nums[j]
|
||||
for j := l.arrSize - 1; j >= index; j-- {
|
||||
l.arr[j+1] = l.arr[j]
|
||||
}
|
||||
l.nums[index] = num
|
||||
l.arr[index] = num
|
||||
// 更新元素数量
|
||||
l.numsSize++
|
||||
l.arrSize++
|
||||
}
|
||||
|
||||
/* 删除元素 */
|
||||
func (l *myList) remove(index int) int {
|
||||
if index < 0 || index >= l.numsSize {
|
||||
if index < 0 || index >= l.arrSize {
|
||||
panic("索引越界")
|
||||
}
|
||||
num := l.nums[index]
|
||||
num := l.arr[index]
|
||||
// 索引 i 之后的元素都向前移动一位
|
||||
for j := index; j < l.numsSize-1; j++ {
|
||||
l.nums[j] = l.nums[j+1]
|
||||
for j := index; j < l.arrSize-1; j++ {
|
||||
l.arr[j] = l.arr[j+1]
|
||||
}
|
||||
// 更新元素数量
|
||||
l.numsSize--
|
||||
l.arrSize--
|
||||
// 返回被删除元素
|
||||
return num
|
||||
}
|
||||
@@ -97,13 +97,13 @@ func (l *myList) remove(index int) int {
|
||||
/* 列表扩容 */
|
||||
func (l *myList) extendCapacity() {
|
||||
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组拷贝到新数组
|
||||
l.nums = append(l.nums, make([]int, l.numsCapacity*(l.extendRatio-1))...)
|
||||
l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)
|
||||
// 更新列表容量
|
||||
l.numsCapacity = len(l.nums)
|
||||
l.arrCapacity = len(l.arr)
|
||||
}
|
||||
|
||||
/* 返回有效长度的列表 */
|
||||
func (l *myList) toArray() []int {
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
return l.nums[:l.numsSize]
|
||||
return l.arr[:l.arrSize]
|
||||
}
|
||||
|
||||
@@ -12,35 +12,35 @@ import (
|
||||
/* Driver Code */
|
||||
func TestMyList(t *testing.T) {
|
||||
/* 初始化列表 */
|
||||
list := newMyList()
|
||||
nums := newMyList()
|
||||
/* 尾部添加元素 */
|
||||
list.add(1)
|
||||
list.add(3)
|
||||
list.add(2)
|
||||
list.add(5)
|
||||
list.add(4)
|
||||
fmt.Printf("列表 list = %v ,容量 = %v ,长度 = %v\n", list.toArray(), list.capacity(), list.size())
|
||||
nums.add(1)
|
||||
nums.add(3)
|
||||
nums.add(2)
|
||||
nums.add(5)
|
||||
nums.add(4)
|
||||
fmt.Printf("列表 nums = %v ,容量 = %v ,长度 = %v\n", nums.toArray(), nums.capacity(), nums.size())
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.insert(6, 3)
|
||||
fmt.Printf("在索引 3 处插入数字 6 ,得到 list = %v\n", list.toArray())
|
||||
nums.insert(6, 3)
|
||||
fmt.Printf("在索引 3 处插入数字 6 ,得到 nums = %v\n", nums.toArray())
|
||||
|
||||
/* 删除元素 */
|
||||
list.remove(3)
|
||||
fmt.Printf("删除索引 3 处的元素,得到 list = %v\n", list.toArray())
|
||||
nums.remove(3)
|
||||
fmt.Printf("删除索引 3 处的元素,得到 nums = %v\n", nums.toArray())
|
||||
|
||||
/* 访问元素 */
|
||||
num := list.get(1)
|
||||
num := nums.get(1)
|
||||
fmt.Printf("访问索引 1 处的元素,得到 num = %v\n", num)
|
||||
|
||||
/* 更新元素 */
|
||||
list.set(0, 1)
|
||||
fmt.Printf("将索引 1 处的元素更新为 0 ,得到 list = %v\n", list.toArray())
|
||||
nums.set(0, 1)
|
||||
fmt.Printf("将索引 1 处的元素更新为 0 ,得到 nums = %v\n", nums.toArray())
|
||||
|
||||
/* 测试扩容机制 */
|
||||
for i := 0; i < 10; i++ {
|
||||
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
|
||||
list.add(i)
|
||||
nums.add(i)
|
||||
}
|
||||
fmt.Printf("扩容后的列表 list = %v ,容量 = %v ,长度 = %v\n", list.toArray(), list.capacity(), list.size())
|
||||
fmt.Printf("扩容后的列表 nums = %v ,容量 = %v ,长度 = %v\n", nums.toArray(), nums.capacity(), nums.size())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user