diff --git a/codes/go/chapter_array_and_linkedlist/array.go b/codes/go/chapter_array_and_linkedlist/array.go index eb21143fe..1f1a78be1 100644 --- a/codes/go/chapter_array_and_linkedlist/array.go +++ b/codes/go/chapter_array_and_linkedlist/array.go @@ -1,6 +1,6 @@ // File: array.go // Created Time: 2022-12-29 -// Author: GuoWei (gongguowei01@gmail.com) +// Author: GuoWei (gongguowei01@gmail.com), cathay (cathaycchen@gmail.com) package chapter_array_and_linkedlist @@ -38,7 +38,6 @@ func extend(nums []int, enlarge int) []int { /* 在数组的索引 index 处插入元素 num */ func insert(nums []int, num int, index int) { // 把索引 index 以及之后的所有元素向后移动一位 - // 如果超出了数组长度,会被直接舍弃 for i := len(nums) - 1; i > index; i-- { nums[i] = nums[i-1] } @@ -49,14 +48,14 @@ func insert(nums []int, num int, index int) { /* 删除索引 index 处元素 */ func remove(nums []int, index int) { // 把索引 index 之后的所有元素向前移动一位 - for i := index; i < len(nums) - 1; i++ { + for i := index; i < len(nums)-1; i++ { nums[i] = nums[i+1] } } /* 遍历数组 */ func traverse(nums []int) { - var count int + count := 0 // 通过索引遍历数组 for i := 0; i < len(nums); i++ { count++ diff --git a/codes/go/chapter_array_and_linkedlist/array_test.go b/codes/go/chapter_array_and_linkedlist/array_test.go index 384cc34d9..7dff6a034 100644 --- a/codes/go/chapter_array_and_linkedlist/array_test.go +++ b/codes/go/chapter_array_and_linkedlist/array_test.go @@ -1,6 +1,6 @@ // File: array_test.go // Created Time: 2022-12-29 -// Author: GuoWei (gongguowei01@gmail.com) +// Author: GuoWei (gongguowei01@gmail.com), cathay (cathaycchen@gmail.com) package chapter_array_and_linkedlist @@ -18,30 +18,30 @@ import ( func TestArray(t *testing.T) { /* 初始化数组 */ var arr []int - fmt.Println("数组 arr = ", arr) + fmt.Println("数组 arr =", arr) nums := []int{1, 3, 2, 5, 4} - fmt.Println("数组 nums = ", nums) + fmt.Println("数组 nums =", nums) /* 随机访问 */ randomNum := randomAccess(nums) - fmt.Println("在 nums 中获取随机元素 ", randomNum) + fmt.Println("在 nums 中获取随机元素", randomNum) /* 长度扩展 */ nums = extend(nums, 3) - fmt.Println("将数组长度扩展至 8 ,得到 nums = ", nums) + fmt.Println("将数组长度扩展至 8 ,得到 nums =", nums) /* 插入元素 */ insert(nums, 6, 3) - fmt.Println("在索引 3 处插入数字 6 ,得到 nums = ", nums) + fmt.Println("在索引 3 处插入数字 6 ,得到 nums =", nums) /* 删除元素 */ remove(nums, 2) - fmt.Println("删除索引 2 处的元素,得到 nums = ", nums) + fmt.Println("删除索引 2 处的元素,得到 nums =", nums) /* 遍历数组 */ traverse(nums) /* 查找元素 */ index := find(nums, 3) - fmt.Println("在 nums 中查找元素 3 ,得到索引 = ", index) + fmt.Println("在 nums 中查找元素 3 ,得到索引 =", index) } diff --git a/codes/go/chapter_array_and_linkedlist/linked_list.go b/codes/go/chapter_array_and_linkedlist/linked_list.go new file mode 100644 index 000000000..106e49f4c --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/linked_list.go @@ -0,0 +1,51 @@ +// File: linked_list.go +// Created Time: 2022-12-29 +// Author: cathay (cathaycchen@gmail.com) + +package chapter_array_and_linkedlist + +import ( + . "github.com/krahets/hello-algo/pkg" +) + +/* 在链表的结点 n0 之后插入结点 P */ +func insertNode(n0 *pkg.ListNode, P *pkg.ListNode) { + n1 := n0.Next + n0.Next = P + P.Next = n1 +} + +/* 删除链表的结点 n0 之后的首个结点 */ +func removeNode(n0 *pkg.ListNode) { + if n0.Next == nil { + return + } + // n0 -> P -> n1 + P := n0.Next + n1 := P.Next + n0.Next = n1 +} + +/* 访问链表中索引为 index 的结点 */ +func access(head *pkg.ListNode, index int) *pkg.ListNode { + for i := 0; i < index; i++ { + head = head.Next + if head == nil { + return nil + } + } + return head +} + +/* 在链表中查找值为 target 的首个结点 */ +func findNode(head *pkg.ListNode, target int) int { + index := 0 + for head != nil { + if head.Val == target { + return index + } + head = head.Next + index++ + } + return -1 +} diff --git a/codes/go/chapter_array_and_linkedlist/linked_list_test.go b/codes/go/chapter_array_and_linkedlist/linked_list_test.go new file mode 100644 index 000000000..18061efff --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/linked_list_test.go @@ -0,0 +1,47 @@ +// File: linked_list_test.go +// Created Time: 2022-12-29 +// Author: cathay (cathaycchen@gmail.com) + +package chapter_array_and_linkedlist + +import ( + "fmt" + "github.com/krahets/hello-algo/pkg" + "testing" +) + +func TestLikedList(t *testing.T) { + /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ + // 初始化各个结点 + n0 := pkg.NewListNode(1) + n1 := pkg.NewListNode(3) + n2 := pkg.NewListNode(2) + n3 := pkg.NewListNode(5) + n4 := pkg.NewListNode(4) + + // 构建引用指向 + n0.Next = n1 + n1.Next = n2 + n2.Next = n3 + n3.Next = n4 + fmt.Println("初始化的链表为") + pkg.PrintLinkedList(n0) + + /* 插入结点 */ + insertNode(n0, pkg.NewListNode(0)) + fmt.Println("插入结点后的链表为") + pkg.PrintLinkedList(n0) + + /* 删除结点 */ + removeNode(n0) + fmt.Println("删除结点后的链表为") + pkg.PrintLinkedList(n0) + + /* 访问结点 */ + node := access(n0, 3) + fmt.Println("链表中索引 3 处的结点的值 =", node) + + /* 查找结点 */ + index := findNode(n0, 2) + fmt.Println("链表中值为 2 的结点的索引 =", index) +} diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index 095960563..5168a5d5d 100644 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -250,8 +250,8 @@ elementAddr = firtstElementAddr + elementLength * elementIndex // 初始化一个扩展长度后的数组 res := make([]int, len(nums)+enlarge) // 将原数组中的所有元素复制到新数组 - for i := 0; i < len(nums); i++ { - res[i] = nums[i] + for i, num := range nums { + res[i] = num } // 返回扩展后的新数组 return res @@ -537,14 +537,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```go title="array.go" /* 遍历数组 */ func traverse(nums []int) { - var count int + count := 0 // 通过索引遍历数组 for i := 0; i < len(nums); i++ { count++ } // 直接遍历数组 - for index, val := range nums { - fmt.Printf("index:%v value:%v\n", index, val) + for range nums { + count++ } } ``` diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 33cfdd346..a55d68028 100644 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -51,7 +51,19 @@ comments: true === "Go" ```go title="" - + /* 链表结点结构体 */ + type ListNode struct { + Val int // 结点值 + Next *ListNode // 指向下一结点的指针(引用) + } + + // NewListNode 构造函数,创建一个新的链表 + func NewListNode(val int) *ListNode { + return &ListNode{ + Val: val, + Next: nil, + } + } ``` === "JavaScript" @@ -162,7 +174,19 @@ comments: true === "Go" ```go title="" - + /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ + // 初始化各个结点 + n0 := NewListNode(1) + n1 := NewListNode(3) + n2 := NewListNode(2) + n3 := NewListNode(5) + n4 := NewListNode(4) + + // 构建引用指向 + n0.Next = n1 + n1.Next = n2 + n2.Next = n3 + n3.Next = n4 ``` === "JavaScript" @@ -294,7 +318,23 @@ comments: true === "Go" ```go title="" + /* 在链表的结点 n0 之后插入结点 P */ + func insert(n0 *ListNode, P *ListNode) { + n1 := n0.Next + n0.Next = P + P.Next = n1 + } + /* 删除链表的结点 n0 之后的首个结点 */ + func removeNode(n0 *ListNode) { + if n0.Next == nil { + return + } + // n0 -> P -> n1 + P := n0.Next + n1 := P.Next + n0.Next = n1 + } ``` === "JavaScript" @@ -415,7 +455,16 @@ comments: true === "Go" ```go title="" - + /* 访问链表中索引为 index 的结点 */ + func access(head *ListNode, index int) *ListNode { + for i := 0; i < index; i++ { + head = head.Next + if head == nil { + return nil + } + } + return head + } ``` === "JavaScript" @@ -524,7 +573,18 @@ comments: true === "Go" ```go title="" - + /* 在链表中查找值为 target 的首个结点 */ + func find(head *ListNode, target int) int { + index := 0 + for head != nil { + if head.Val == target { + return index + } + head = head.Next + index++ + } + return -1 + } ``` === "JavaScript" @@ -631,7 +691,21 @@ comments: true === "Go" ```go title="" - + /* 双向链表结点结构体 */ + type DoublyListNode struct { + Val int // 结点值 + Next *DoublyListNode // 指向后继结点的指针(引用) + Prev *DoublyListNode // 指向前驱结点的指针(引用) + } + + // NewDoublyListNode 初始化 + func NewDoublyListNode(val int) *DoublyListNode { + return &DoublyListNode{ + Val: val, + Next: nil, + Prev: nil, + } + } ``` === "JavaScript"