type Thing struct {
Id int
Info string
}
func main{
doubleList := list.New() //生成一个双向链表的对象
doubleList.PushBack(&Thing{Id: 1, Info: "hello"}) //想链表后端添加数据,数据的来源是刚刚定义好的结构体类型.(这里需要添加指针类型,否则无法添加到链表对应的内存空间)
doubleList.PushBack(&Thing{Id: 2, Info: "list"})
doubleList.PushBack(&Thing{Id: 3, Info: "!"})
for e := doubleList.Front(); e != nil; e = e.Next() { //遍历链表内容.
v := (e.Value).(*Thing) //这里貌似需要做一个类型断言.??
if v.Id == 2 {
doubleList.Remove(e) //将需要移除的链表节点移除
}
fmt.Printf("v:%v\n", v)
}
//尾部添加
doubleList.PushBack(&Thing{
Id: 2,
Info: "list",
})
//头部添加
doubleList.PushFront(&Thing{
Id: 0,
Info: "0",
})
for i := doubleList.Front(); i != nil; i = i.Next() {
fmt.Println("链表:", i.Value)
}
}