mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 06:07:20 +08:00 
			
		
		
		
	fix(array and linkedlist): fix that the printing in the test function is the same as that in other languages
This commit is contained in:
		@ -9,10 +9,6 @@ import (
 | 
			
		||||
	"math/rand"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
/* 初始化数组 */
 | 
			
		||||
var arr = [5]int{}
 | 
			
		||||
var nums = []int{1, 3, 2, 5, 4}
 | 
			
		||||
 | 
			
		||||
/* 随机返回一个数组元素 */
 | 
			
		||||
func randomAccess(nums []int) int {
 | 
			
		||||
	randomIndex := rand.Intn(len(nums))
 | 
			
		||||
@ -33,9 +29,9 @@ func extend(nums []int, enlarge int) []int {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 在数组的索引 index 处插入元素 num */
 | 
			
		||||
func insert(nums []int, size int, num int, index int) {
 | 
			
		||||
func insert(nums []int, num int, index int) {
 | 
			
		||||
	// 把索引 index 以及之后的所有元素向后移动一位
 | 
			
		||||
	for i := size - 1; i > index; i-- {
 | 
			
		||||
	for i := len(nums) - 1; i > index; i-- {
 | 
			
		||||
		nums[i] = nums[i-1]
 | 
			
		||||
	}
 | 
			
		||||
	// 将 num 赋给 index 处元素
 | 
			
		||||
@ -43,9 +39,9 @@ func insert(nums []int, size int, num int, index int) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 删除索引 index 处元素 */
 | 
			
		||||
func remove(nums []int, size int, index int) {
 | 
			
		||||
func remove(nums []int, index int) {
 | 
			
		||||
	// 把索引 index 之后的所有元素向前移动一位
 | 
			
		||||
	for i := index; i < size-1; i++ {
 | 
			
		||||
	for i := index; i < len(nums)-1; i++ {
 | 
			
		||||
		nums[i] = nums[i+1]
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -9,20 +9,35 @@ import (
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
func TestArray(t *testing.T) {
 | 
			
		||||
	nums := make([]int, 5)
 | 
			
		||||
	fmt.Println("randomAccess:", randomAccess(nums))
 | 
			
		||||
	/* 初始化数组 */
 | 
			
		||||
	var arr = [5]int{}
 | 
			
		||||
	fmt.Println("数组 arr =", arr)
 | 
			
		||||
 | 
			
		||||
	fmt.Println("extend:", extend(nums, 5))
 | 
			
		||||
	var nums = []int{1, 3, 2, 5, 4}
 | 
			
		||||
	fmt.Println("数组 nums =", nums)
 | 
			
		||||
 | 
			
		||||
	insert(nums, 5, 2, 2)
 | 
			
		||||
	fmt.Println("after insert:", nums)
 | 
			
		||||
	/* 随机访问 */
 | 
			
		||||
	randomNum := randomAccess(nums)
 | 
			
		||||
	fmt.Println("在 nums 中获取随机元素", randomNum)
 | 
			
		||||
 | 
			
		||||
	remove(nums, 5, 2)
 | 
			
		||||
	fmt.Println("after remove:", nums)
 | 
			
		||||
	/* 长度扩展 */
 | 
			
		||||
	nums = extend(nums, 3)
 | 
			
		||||
	fmt.Println("将数组长度扩展至 8 ,得到 nums =", nums)
 | 
			
		||||
 | 
			
		||||
	fmt.Println("traverse nums:")
 | 
			
		||||
	/* 插入元素 */
 | 
			
		||||
	insert(nums, 6, 3)
 | 
			
		||||
	fmt.Println("在索引 3 处插入数字 6 ,得到 nums =", nums)
 | 
			
		||||
 | 
			
		||||
	/* 删除元素 */
 | 
			
		||||
	remove(nums, 2)
 | 
			
		||||
	fmt.Println("删除索引 2 处的元素,得到 nums = ", nums)
 | 
			
		||||
 | 
			
		||||
	/* 遍历数组 */
 | 
			
		||||
	traverse(nums)
 | 
			
		||||
 | 
			
		||||
	fmt.Println("find value 2 key:", find(nums, 2))
 | 
			
		||||
	/* 查找元素 */
 | 
			
		||||
	index := find(nums, 3)
 | 
			
		||||
	fmt.Println("在 nums 中查找元素 3 ,得到索引 =", index)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -5,48 +5,18 @@
 | 
			
		||||
package chapter_array_and_linkedlist
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"github.com/krahets/hello-algo/pkg"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
/* 链表结点结构体 */
 | 
			
		||||
type ListNode struct {
 | 
			
		||||
	Val  int       // 结点值
 | 
			
		||||
	Next *ListNode // 指向下一结点的指针(引用)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewListNode 构造函数,创建一个新的链表
 | 
			
		||||
func NewListNode(val int) *ListNode {
 | 
			
		||||
	return &ListNode{
 | 
			
		||||
		Val:  val,
 | 
			
		||||
		Next: nil,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PrintLinkedList Print a linked list
 | 
			
		||||
func PrintLinkedList(node *ListNode) {
 | 
			
		||||
	if node == nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	var builder strings.Builder
 | 
			
		||||
	for node.Next != nil {
 | 
			
		||||
		builder.WriteString(strconv.Itoa(node.Val) + " -> ")
 | 
			
		||||
		node = node.Next
 | 
			
		||||
	}
 | 
			
		||||
	builder.WriteString(strconv.Itoa(node.Val))
 | 
			
		||||
	fmt.Println(builder.String())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 在链表的结点 n0 之后插入结点 P */
 | 
			
		||||
func insertNode(n0 *ListNode, P *ListNode) {
 | 
			
		||||
func insertNode(n0 *pkg.ListNode, P *pkg.ListNode) {
 | 
			
		||||
	n1 := n0.Next
 | 
			
		||||
	n0.Next = P
 | 
			
		||||
	P.Next = n1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 删除链表的结点 n0 之后的首个结点 */
 | 
			
		||||
func removeNode(n0 *ListNode) {
 | 
			
		||||
func removeNode(n0 *pkg.ListNode) {
 | 
			
		||||
	if n0.Next == nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
@ -57,7 +27,7 @@ func removeNode(n0 *ListNode) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 访问链表中索引为 index 的结点 */
 | 
			
		||||
func access(head *ListNode, index int) *ListNode {
 | 
			
		||||
func access(head *pkg.ListNode, index int) *pkg.ListNode {
 | 
			
		||||
	for i := 0; i < index; i++ {
 | 
			
		||||
		head = head.Next
 | 
			
		||||
		if head == nil {
 | 
			
		||||
@ -68,7 +38,7 @@ func access(head *ListNode, index int) *ListNode {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 在链表中查找值为 target 的首个结点 */
 | 
			
		||||
func findNode(head *ListNode, target int) int {
 | 
			
		||||
func findNode(head *pkg.ListNode, target int) int {
 | 
			
		||||
	index := 0
 | 
			
		||||
	for head != nil {
 | 
			
		||||
		if head.Val == target {
 | 
			
		||||
@ -79,27 +49,3 @@ func findNode(head *ListNode, target int) int {
 | 
			
		||||
	}
 | 
			
		||||
	return -1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 双向链表结点结构体 */
 | 
			
		||||
type DoublyListNode struct {
 | 
			
		||||
	Val  int             // 结点值
 | 
			
		||||
	Next *DoublyListNode // 指向后继结点的指针(引用)
 | 
			
		||||
	Prev *DoublyListNode // 指向前驱结点的指针(引用)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewDoublyListNode 初始化
 | 
			
		||||
func NewDoublyListNode(val int, nodes ...*DoublyListNode) *DoublyListNode {
 | 
			
		||||
	var next, prev *DoublyListNode
 | 
			
		||||
	length := len(nodes)
 | 
			
		||||
	if length > 0 {
 | 
			
		||||
		next = nodes[0]
 | 
			
		||||
	}
 | 
			
		||||
	if length > 1 {
 | 
			
		||||
		prev = nodes[1]
 | 
			
		||||
	}
 | 
			
		||||
	return &DoublyListNode{
 | 
			
		||||
		Val:  val,
 | 
			
		||||
		Next: next,
 | 
			
		||||
		Prev: prev,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -6,42 +6,42 @@ 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 := NewListNode(1)
 | 
			
		||||
	n1 := NewListNode(3)
 | 
			
		||||
	n2 := NewListNode(2)
 | 
			
		||||
	n3 := NewListNode(5)
 | 
			
		||||
	n4 := NewListNode(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)
 | 
			
		||||
 | 
			
		||||
	fmt.Println("链表结构:")
 | 
			
		||||
	PrintLinkedList(n0)
 | 
			
		||||
	/* 插入结点 */
 | 
			
		||||
	insertNode(n0, pkg.NewListNode(0))
 | 
			
		||||
	fmt.Println("插入结点后的链表为")
 | 
			
		||||
	pkg.PrintLinkedList(n0)
 | 
			
		||||
 | 
			
		||||
	P := NewListNode(6)
 | 
			
		||||
 | 
			
		||||
	/* 在链表的结点 n0 之后插入结点 P */
 | 
			
		||||
	insertNode(n0, P)
 | 
			
		||||
	fmt.Println("在链表的结点 n0 之后插入结点 P 后,链表结构:")
 | 
			
		||||
	PrintLinkedList(n0)
 | 
			
		||||
 | 
			
		||||
	/* 删除链表的结点 n0 之后的首个结点 */
 | 
			
		||||
	/* 删除结点 */
 | 
			
		||||
	removeNode(n0)
 | 
			
		||||
	fmt.Println("删除链表的结点 n0 之后的首个结点后,链表结构:")
 | 
			
		||||
	PrintLinkedList(n0)
 | 
			
		||||
	fmt.Println("删除结点后的链表为")
 | 
			
		||||
	pkg.PrintLinkedList(n0)
 | 
			
		||||
 | 
			
		||||
	/* 访问链表中索引为 index 的结点 */
 | 
			
		||||
	fmt.Println("访问链表中索引为 2 的结点:", access(n0, 2))
 | 
			
		||||
	/* 访问结点 */
 | 
			
		||||
	node := access(n0, 3)
 | 
			
		||||
	fmt.Println("链表中索引 3 处的结点的值 =", node)
 | 
			
		||||
 | 
			
		||||
	/* 在链表中查找值为 target 的首个结点 */
 | 
			
		||||
	fmt.Println("在链表中查找 5 的首个节点索引值:", findNode(n0, 5))
 | 
			
		||||
	/* 查找结点 */
 | 
			
		||||
	index := findNode(n0, 2)
 | 
			
		||||
	fmt.Println("链表中值为 2 的结点的索引 =", index)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user