-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from Cathay-Chen/master
Add the Go code to array and linked_list docs (Chapter of Array and LinkedList)
- Loading branch information
Showing
6 changed files
with
193 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters