Skip to content

Commit

Permalink
Merge pull request #188 from Cathay-Chen/master
Browse files Browse the repository at this point in the history
Add the Go code to array and linked_list docs (Chapter of Array and LinkedList)
  • Loading branch information
krahets authored Jan 2, 2023
2 parents 5ea660a + 82b8a5d commit fd4737e
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 22 deletions.
7 changes: 3 additions & 4 deletions codes/go/chapter_array_and_linkedlist/array.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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]
}
Expand All @@ -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++
Expand Down
16 changes: 8 additions & 8 deletions codes/go/chapter_array_and_linkedlist/array_test.go
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
}
51 changes: 51 additions & 0 deletions codes/go/chapter_array_and_linkedlist/linked_list.go
Original file line number Diff line number Diff line change
@@ -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
}
47 changes: 47 additions & 0 deletions codes/go/chapter_array_and_linkedlist/linked_list_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 5 additions & 5 deletions docs/chapter_array_and_linkedlist/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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++
}
}
```
Expand Down
84 changes: 79 additions & 5 deletions docs/chapter_array_and_linkedlist/linked_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit fd4737e

Please sign in to comment.