2022-11-25 20:24:51 +08:00
|
|
|
// File: linear_search_test.go
|
|
|
|
// Created Time: 2022-11-25
|
|
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
|
|
|
|
package chapter_searching
|
|
|
|
|
|
|
|
import (
|
2022-12-03 20:25:24 +08:00
|
|
|
"fmt"
|
2022-11-25 20:24:51 +08:00
|
|
|
"testing"
|
2022-11-27 04:19:16 +08:00
|
|
|
|
|
|
|
. "github.com/krahets/hello-algo/pkg"
|
2022-11-25 20:24:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLinearSearch(t *testing.T) {
|
|
|
|
target := 3
|
|
|
|
nums := []int{1, 5, 3, 2, 4, 7, 5, 9, 10, 8}
|
|
|
|
|
|
|
|
// 在数组中执行线性查找
|
|
|
|
index := linerSearchArray(nums, target)
|
2022-12-03 20:25:24 +08:00
|
|
|
fmt.Println("目标元素 3 的索引 =", index)
|
2022-11-25 20:24:51 +08:00
|
|
|
|
|
|
|
// 在链表中执行线性查找
|
2022-11-26 01:17:48 +08:00
|
|
|
head := ArrayToLinkedList(nums)
|
2022-11-25 20:24:51 +08:00
|
|
|
node := linerSearchLinkedList(head, target)
|
2022-12-03 20:25:24 +08:00
|
|
|
fmt.Println("目标结点值 3 的对应结点对象为", node)
|
2022-11-25 20:24:51 +08:00
|
|
|
}
|