mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-09 14:06:06 +00:00
refactor: Replace 结点 with 节点 (#452)
* Replace 结点 with 节点 Update the footnotes in the figures * Update mindmap * Reduce the size of the mindmap.png
This commit is contained in:
@@ -8,14 +8,14 @@ import (
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
func insertNode(n0 *ListNode, P *ListNode) {
|
||||
n1 := n0.Next
|
||||
P.Next = n1
|
||||
n0.Next = P
|
||||
}
|
||||
|
||||
/* 删除链表的结点 n0 之后的首个结点 */
|
||||
/* 删除链表的节点 n0 之后的首个节点 */
|
||||
func removeNode(n0 *ListNode) {
|
||||
if n0.Next == nil {
|
||||
return
|
||||
@@ -26,7 +26,7 @@ func removeNode(n0 *ListNode) {
|
||||
n0.Next = n1
|
||||
}
|
||||
|
||||
/* 访问链表中索引为 index 的结点 */
|
||||
/* 访问链表中索引为 index 的节点 */
|
||||
func access(head *ListNode, index int) *ListNode {
|
||||
for i := 0; i < index; i++ {
|
||||
if head == nil {
|
||||
@@ -37,7 +37,7 @@ func access(head *ListNode, index int) *ListNode {
|
||||
return head
|
||||
}
|
||||
|
||||
/* 在链表中查找值为 target 的首个结点 */
|
||||
/* 在链表中查找值为 target 的首个节点 */
|
||||
func findNode(head *ListNode, target int) int {
|
||||
index := 0
|
||||
for head != nil {
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
func TestLinkedList(t *testing.T) {
|
||||
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
||||
// 初始化各个结点
|
||||
// 初始化各个节点
|
||||
n0 := NewListNode(1)
|
||||
n1 := NewListNode(3)
|
||||
n2 := NewListNode(2)
|
||||
@@ -28,21 +28,21 @@ func TestLinkedList(t *testing.T) {
|
||||
fmt.Println("初始化的链表为")
|
||||
PrintLinkedList(n0)
|
||||
|
||||
/* 插入结点 */
|
||||
/* 插入节点 */
|
||||
insertNode(n0, NewListNode(0))
|
||||
fmt.Println("插入结点后的链表为")
|
||||
fmt.Println("插入节点后的链表为")
|
||||
PrintLinkedList(n0)
|
||||
|
||||
/* 删除结点 */
|
||||
/* 删除节点 */
|
||||
removeNode(n0)
|
||||
fmt.Println("删除结点后的链表为")
|
||||
fmt.Println("删除节点后的链表为")
|
||||
PrintLinkedList(n0)
|
||||
|
||||
/* 访问结点 */
|
||||
/* 访问节点 */
|
||||
node := access(n0, 3)
|
||||
fmt.Println("链表中索引 3 处的结点的值 =", node)
|
||||
fmt.Println("链表中索引 3 处的节点的值 =", node)
|
||||
|
||||
/* 查找结点 */
|
||||
/* 查找节点 */
|
||||
index := findNode(n0, 2)
|
||||
fmt.Println("链表中值为 2 的结点的索引 =", index)
|
||||
fmt.Println("链表中值为 2 的节点的索引 =", index)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user