golang数据结构之List


golang数据结构之List,实际中用得很少,这里只做研究。

package main

import (
	"container/list"
	"github.com/sanity-io/litter"
)

type Thing struct {
	Id   int
	Info string
}

// golang 双向链表结构
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() {
	//	litter.Dump(e.Value)
	//}

	for e := doubleList.Front(); e != nil; e = e.Next() {
		v := (e.Value).(*Thing)
		if v.Id == 2 {
			doubleList.Remove(e)
		}
	}

	for e := doubleList.Front(); e != nil; e = e.Next() {
		litter.Dump(e.Value)
	}

	// json无法解析doubleList
	//data, err := json.Marshal(doubleList)
	//if err == nil {
	//	//litter.Dump(string(data))
	//}

	doubleList.PushBack(&Thing{Id:2, Info:"list"})
	//for e := doubleList.Front(); e != nil; e = e.Next() {
	//	litter.Dump(e.Value)
	//}

	doubleList.PushFront(&Thing{Id:0, Info:"0"})
	//for e := doubleList.Front(); e != nil; e = e.Next() {
	//	litter.Dump(e.Value)
	//}

	doubleList.PushBackList(doubleList)
	for e := doubleList.Front(); e != nil; e = e.Next() {
		litter.Dump(e.Value)
	}

	doubleList.PushFrontList(doubleList)

	for e := doubleList.Front(); e != nil; e = e.Next() {
		litter.Dump(e.Value)
	}

}

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM