golang的list数据结构demo

package main
import "container/list"
func main(){    var mylist list.List
//放在尾部 mylist.PushBack("go") mylist.PushBack("grpc") mylist.PushBack("mysql")
//头部放数据 mylist.PushFront("gin")
//在grpc前插入元素java i := mylist.Front() for ; i != nil; i = i.Next() { if i.Value.(string) == "grpc" { break } } mylist.InsertBefore("java", i)
//删除grpc元素 i := mylist.Front() for ; i != nil; i = i.Next() { if i.Value.(string) == "grpc" { break } } mylist.Remove(i)
//遍历打印值,正序 for i := mylist.Front(); i != nil; i = i.Next() { fmt.Println(i.Value) }
//反向遍历 for i := mylist.Back(); i != nil; i = i.Prev() { fmt.Println(i.Value) }
}

 

posted @ 2023-07-12 20:05  nLesxw  阅读(23)  评论(0编辑  收藏  举报