package test;
import (
"testing"
"fmt"
)
type InteegetCell struct{
Value int
Next *InteegetCell
}
// 初始化链表(建立一个头指针)
func initCell() *InteegetCell{
return new(InteegetCell)
}
// 头插法追加元素
func AddAtBeginning(top *InteegetCell, value int){
newCell := &InteegetCell{value,top.Next}
top.Next = newCell
}
// 尾插法的实现
func AddAtTali(top *InteegetCell, value int){
for top.Next != nil {
top = top.Next
}
newCell := &InteegetCell{value,nil}
top.Next = newCell
}
// 变历链表
func Interte(top *InteegetCell){
var result []int
for top != nil {
value := top.Value
result = append(result, value)
top = top.Next
}
fmt.Println(result)
}
func Test(t *testing.T){
top := initCell()
// fmt.Println("-----测试头插法-----")
// for i:=1; i<4; i++ {
// AddAtBeginning(top,i)
// }
// // 遍历链表
// Interte(top)
fmt.Println("-----测试尾插法-----")
AddAtTali(top,1)
AddAtTali(top,2)
AddAtTali(top,3)
Interte(top)
}
转载于:https://www.cnblogs.com/jzsg/p/10963916.html