From ba70d98f64c0a5f257dbbb543352c760787ad8ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=B4=E6=9E=97=E5=B3=B0?= Date: Fri, 23 Dec 2022 18:42:24 +0800 Subject: [PATCH] update linked_list go code --- .../linked_list.go | 52 +++++++++++++++ .../linked_list_test.go | 31 +++++++++ .../linked_list.md | 66 +++++++++++++++++-- 3 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 codes/go/chapter_array_and_linkedlist/linked_list.go create mode 100644 codes/go/chapter_array_and_linkedlist/linked_list_test.go 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..c3b989e93 --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/linked_list.go @@ -0,0 +1,52 @@ +// File: linked_list.go +// Created Time: 2022-12-23 +// Author: dlfld (2441086385@qq.com) + +package chapter_array_and_linkedlist +/* 链表结点结构体 */ +type ListNode struct { + Val int // 节点值 + Next *ListNode // 指向下一结点的指针(引用) + } + + /* 在链表的结点 n0 之后插入结点 P */ + func insert(n0, P *ListNode) { + n1 := n0.Next + n0.Next = P + P.Next = n1 + } + + /* 删除链表的结点 n0 之后的首个结点 */ + func remove(n0 *ListNode) { + if n0.Next == nil { + return + } + // n0 -> P -> n1 + P := n0.Next + n1 := P.Next + n0.Next = n1 + } + + /* 访问链表中索引为 index 的结点 */ + func access(head *ListNode, index int) *ListNode { + for i := 0; i < index; i++ { + head = head.Next + if head == nil { + return nil + } + } + return head + } + + /* 在链表中查找值为 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 + } \ No newline at end of file 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..05d37da42 --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/linked_list_test.go @@ -0,0 +1,31 @@ +// File: linked_list_test.go +// Created Time: 2022-12-23 +// Author: dlfld (2441086385@qq.com) +package chapter_array_and_linkedlist + +import ( +// "fmt" + "fmt" + "testing" +) + +/* Driver Code */ +func TestLinkedList(t *testing.T) { + /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ + // 初始化各个结点 + n0 := &ListNode{1,nil} + n1 := &ListNode{2,nil} + n2 := &ListNode{3,nil} + n3 := &ListNode{4,nil} + n4 := &ListNode{5,nil} + // 构建引用指向 + n0.Next = n1 + n1.Next = n2 + n2.Next = n3 + n3.Next = n4 + + for n0 != nil{ + fmt.Printf("%v ",n0.Val) + n0 = n0.Next + } +} \ No newline at end of file diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 412d09e79..6cb070d4e 100644 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -51,7 +51,11 @@ comments: true === "Go" ```go title="" - + /* 链表结点结构体 */ + type ListNode struct { + Val int // 节点值 + Next *ListNode // 指向下一结点的指针(引用) + } ``` === "JavaScript" @@ -162,7 +166,18 @@ comments: true === "Go" ```go title="" - + /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ + // 初始化各个结点 + n0 := &ListNode{1,nil} + n1 := &ListNode{2,nil} + n2 := &ListNode{3,nil} + n3 := &ListNode{4,nil} + n4 := &ListNode{5,nil} + // 构建引用指向 + n0.Next = n1 + n1.Next = n2 + n2.Next = n3 + n3.Next = n4 ``` === "JavaScript" @@ -294,7 +309,23 @@ comments: true === "Go" ```go title="" + /* 在链表的结点 n0 之后插入结点 P */ + func insert(n0, P *ListNode) { + n1 := n0.Next + n0.Next = P + P.Next = n1 + } + /* 删除链表的结点 n0 之后的首个结点 */ + func remove(n0 *ListNode) { + if n0.Next == nil { + return + } + // n0 -> P -> n1 + P := n0.Next + n1 := P.Next + n0.Next = n1 + } ``` === "JavaScript" @@ -415,7 +446,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 +564,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 +682,12 @@ comments: true === "Go" ```go title="" - + /* 双向链表结点类 */ + type ListNode struct { + Val int // 节点值 + Prev *ListNode // 指向前驱结点的指针(引用) + Next *ListNode // 指向后继结点的指针(引用) + } ``` === "JavaScript"