feat(Go-Tool):2020/12/7 :添加container包使用示例和源码分析

This commit is contained in:
Huangzj
2020-12-07 15:00:24 +08:00
parent 55d6488fc3
commit 23d7d6e226
8 changed files with 845 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
/*
* @Author : huangzj
* @Time : 2020/12/2 15:39
* @Description
*/
package testHeap
type Person struct {
Name string //名字
Age int //年龄
Money float64 //身价
}
type heapTool []*Person
func (h *heapTool) Less(i, j int) bool {
return (*h)[i].Age < (*h)[j].Age
}
func (h *heapTool) Swap(i, j int) {
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}
func (h *heapTool) Len() int {
return len(*h)
}
func (h *heapTool) Pop() (v interface{}) {
*h, v = (*h)[:h.Len()-1], (*h)[h.Len()-1]
return
}
func (h *heapTool) Push(v interface{}) {
*h = append(*h, v.(*Person))
}
+62
View File
@@ -0,0 +1,62 @@
/*
* @Author : huangzj
* @Time : 2020/12/2 11:19
* @Description
*/
package testHeap
import (
"container/heap"
"fmt"
"testing"
)
func TestHeapTool(t *testing.T) {
personList := new(heapTool)
personList.Push(&Person{
Name: "小明",
Age: 20,
Money: 100000.99,
})
personList.Push(&Person{
Name: "小施",
Age: 30,
Money: 1002341.99,
})
personList.Push(&Person{
Name: "小康",
Age: 10,
Money: 200.99,
})
personList.Push(&Person{
Name: "老施",
Age: 50,
Money: 10343240000.99,
})
personList.Push(&Person{
Name: "老康",
Age: 70,
Money: 10340.99,
})
personList.Push(&Person{
Name: "老明",
Age: 80,
Money: 13240000.99,
})
personList.Push(&Person{
Name: "老林",
Age: 90,
Money: 10340000.99,
})
heap.Init(personList)
for personList.Len() > 0 {
pop := heap.Pop(personList)
fmt.Println(fmt.Sprintf("%v,%v岁,资产:%v", pop.(*Person).Name, pop.(*Person).Age, pop.(*Person).Money))
}
}
+69
View File
@@ -0,0 +1,69 @@
/*
* @Author : huangzj
* @Time : 2020/12/3 21:58
* @Description:测试list.go包的元素
*/
package testHeap
import (
"container/list"
"fmt"
"testing"
)
func TestList(t *testing.T) {
list2 := list.New()
list2.PushFront(Person{
Name: "康康",
Age: 10,
Money: 20,
})
list2.PushBack(Person{
Name: "老施",
Age: 40,
Money: 1000000,
})
for e := list2.Front(); e != nil; e = e.Next() {
value := e.Value.(Person)
fmt.Println(fmt.Sprintf("名字:%v,年龄:%v,身家:%v", value.Name, value.Age, value.Money))
}
fmt.Println()
fmt.Println()
frontE := list2.Front()
list2.MoveToBack(frontE)
for e := list2.Front(); e != nil; e = e.Next() {
value := e.Value.(Person)
fmt.Println(fmt.Sprintf("名字:%v,年龄:%v,身家:%v", value.Name, value.Age, value.Money))
}
fmt.Println()
fmt.Println()
xiaozhang := list2.InsertBefore(Person{
Name: "小张",
Age: 10,
Money: 50,
}, list2.Front())
for e := list2.Front(); e != nil; e = e.Next() {
value := e.Value.(Person)
fmt.Println(fmt.Sprintf("名字:%v,年龄:%v,身家:%v", value.Name, value.Age, value.Money))
}
fmt.Println()
fmt.Println()
list2.Remove(xiaozhang)
for e := list2.Front(); e != nil; e = e.Next() {
value := e.Value.(Person)
fmt.Println(fmt.Sprintf("名字:%v,年龄:%v,身家:%v", value.Name, value.Age, value.Money))
}
fmt.Println()
fmt.Println()
front := list2.Front().Value.(Person)
fmt.Println(fmt.Sprintf("名字:%v,年龄:%v,身家:%v", front.Name, front.Age, front.Money))
}
+114
View File
@@ -0,0 +1,114 @@
/*
* @Author : huangzj
* @Time : 2020/12/7 9:33
* @Description
*/
package testHeap
import (
"container/ring"
"fmt"
"testing"
)
func TestRing(t *testing.T) {
fmt.Println("测试空链表")
var rings ring.Ring
fmt.Println(rings.Next().Value)
fmt.Println("")
fmt.Println("")
fmt.Println("测试环")
newRing := makeN(10)
Print(newRing)
fmt.Println("")
fmt.Println("")
fmt.Println("测试环Link自己")
newRing = makeN(10)
newRing = newRing.Link(newRing)
Print(newRing)
fmt.Println()
fmt.Println()
fmt.Println("测试环Link其他环")
newRing = makeN(10)
newRing1 := makeN(5)
newRing = newRing.Link(newRing1)
Print(newRing)
fmt.Println()
fmt.Println()
fmt.Println("测试Move")
newRing1 = makeN(5)
newRing1 = newRing1.Move(3)
Print(newRing1)
fmt.Println()
fmt.Println()
fmt.Println("测试Do,没有办法改变环中的元素.")
newRing1 = makeN(5)
newRing1.Do(func(i interface{}) {
i = i.(int) + 1000
})
Print(newRing1)
newRingx := ring.New(3)
for i := 1; i <= newRingx.Len(); i++ {
newRingx.Value = Person{
Name: "小明",
Age: 100,
Money: 19999,
}
newRingx = newRingx.Next()
newRingx.Value = Person{
Name: "小康",
Age: 50,
Money: 19921999,
}
newRingx = newRingx.Next()
newRingx.Value = Person{
Name: "小施",
Age: 20,
Money: 1111999,
}
}
fmt.Println("测试Do,只能对元素进行提取或者输出.")
s := make([]int, 0)
newRingx.Do(func(i interface{}) {
s = append(s, i.(Person).Age)
})
for _, item := range s {
fmt.Println(item)
}
fmt.Println()
fmt.Println()
fmt.Println("测试Unlink")
newRing1 = makeN(5)
newRing2 := newRing1.Unlink(2)
Print(newRing1)
println()
fmt.Println("如果用赋值语句,可以返回被移除的元素")
Print(newRing2)
}
func Print(r *ring.Ring) {
i, n := 0, r.Len()
for p := r; i < n; p = p.Next() {
fmt.Println(fmt.Sprintf("当前元素是%v", p.Value))
i++
}
}
//创造对应的链表
func makeN(n int) *ring.Ring {
r := ring.New(n)
for i := 1; i <= n; i++ {
r.Value = i
r = r.Next()
}
return r
}