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)
	}

}

  

posted @ 2018-08-10 09:51  星空42  阅读(3053)  评论(0编辑  收藏  举报