当前位置:网站首页>【Yugong Series】July 2022 Go Teaching Course 023-List of Go Containers
【Yugong Series】July 2022 Go Teaching Course 023-List of Go Containers
2022-07-31 17:52:00 【HUAWEI CLOUD】
一、Golist of containers
1.列表的定义
A list is a data structure,By multiple elements into a finite sequence,That is, a collection of data items arranged in a certain linear order,Elements can be inserted into this data structure,删除,修改,和查找.
列表的两种主要表现是数组和链表,栈和队列是两种特殊类型的列表.列表(list)The underlying common data structures are: 单链表、双链表等.
2.Go中的列表
在 Go 语言中,List implementations are in container/list 包中,The internal implementation principle is a double linked list.
列表(list)Can easily and efficiently delete elements、插入操作.
3.列表的声明
变量名 := list.New()//或var 变量名 = list.List
4.列表的操作
4.1 列表添加元素
Add elements to the list as follows:
方法 | 功能 |
---|---|
InsertAfter(v interface{}, mark *Element) *Element | 在 mark Insert element points behind |
InsertBefore(v interface{}, mark *Element) *Element | 在 mark Insert element before point |
PushFrontList(other *List) | 添加 other elements in the list to the head |
PushBackList(other *List) | 添加 other elements in the list to the end |
相关案例:
package mainimport ( "container/list" "fmt")func main() { l := list.New() l.PushFront("Head Yugong") l.PushBack("Tail Yugong") // 遍历 for i := l.Front(); i != nil; i = i.Next() { fmt.Println(i.Value) }}
其中 i := l.Front() Said the initial assignment,The head subscript used to get the list;
Then each time the loop will judge i != nil,若等于空,则会退出循环,否则执行 i.Next()Continue looping to the next element;
4.1 列表删除元素
package mainimport ( "container/list" "fmt")func main() { l := list.New() // 头部添加字符串 l.PushFront("愚公1号") // 尾部添加字符串 l.PushBack("愚公2号") // Add an integer to the end,and keep the element handle element := l.PushBack(1) // 在 1 add string after 2 l.InsertAfter("2", element) // 在 1 add string before 0 l.InsertBefore("0", element) for i := l.Front(); i != nil; i = i.Next() { fmt.Println(i.Value) } // 删除 element 对应的元素 l.Remove(element) // 遍历 for i := l.Front(); i != nil; i = i.Next() { fmt.Println(i.Value) }}
4.3 列表获取元素
4.3.1 To obtain a list of nodes
package mainimport ( "container/list" "fmt")func main() { l := list.New() // 头部添加字符串 l.PushFront("愚公1号") // 尾部添加字符串 l.PushBack("愚公2号") // Add an integer to the end,and keep the element handle element := l.PushBack(1) // 在 1 add string after 2 l.InsertAfter("2", element) fmt.Println("Front =", l.Front().Value)}
4.3.2 To obtain a list of nodes
package mainimport ( "container/list" "fmt")func main() { l := list.New() // 头部添加字符串 l.PushFront("愚公1号") // 尾部添加字符串 l.PushBack("愚公2号") // Add an integer to the end,and keep the element handle element := l.PushBack(1) // 在 1 add string after 2 l.InsertAfter("2", element) fmt.Println("Front =", l.Back().Value)}
4.3.3 获取上一个结点
package mainimport ( "container/list" "fmt")func main() { //Use list built-in Prev() 函数,Get the previous node of the list listHaiCoder := list.New() listHaiCoder.PushFront("1") element := listHaiCoder.PushFront("2") //定义节点位置 listHaiCoder.PushFront("3") preElement := element.Prev() fmt.Println("preElement =", preElement.Value)}
4.3.4 获取下一个结点
package mainimport ( "container/list" "fmt")func main() { //Use list built-in Prev() 函数,Get the previous node of the list listHaiCoder := list.New() listHaiCoder.PushFront("1") element := listHaiCoder.PushFront("2") //定义节点位置 listHaiCoder.PushFront("3") preElement := element.Next() fmt.Println("preElement =", preElement.Value)}
边栏推荐
猜你喜欢
AcWing 1282. 搜索关键词 题解((AC自动机)Trie+KMP)+bfs)
Huawei's top engineers lasted nine years "anecdotal stories network protocol" PDF document summary, is too strong
MySQL---Subqueries
这位985教授火了!当了10年博导,竟无一博士毕业!
联邦学习:联邦场景下的多源知识图谱嵌入
ThreadLocal
新型电信“套路”,我爸中招了!
华为手机一键开启“维修模式”隐藏所有数据,让手机隐私更加安全
20.支持向量机—数学原理知识
2022 Android interview summary (with interview questions | source code | interview materials)
随机推荐
mysql的备份表的几种方法
Chinese encoding Settings and action methods return values
浅谈网络安全之算法安全
2022 Android interview summary (with interview questions | source code | interview materials)
Golang 切片删除指定元素的几种方法
iNeuOS工业互联网操作系统,设备运维业务和“低代码”表单开发工具
智能垃圾桶(八)——红外对管传感器(树莓派pico)
MySQL---Subqueries
AcWing 1282. 搜索关键词 题解((AC自动机)Trie+KMP)+bfs)
【pytorch】1.7 pytorch与numpy,tensor与array的转换
35道MySQL面试必问题图解,这样也太好理解了吧
中文编码的设置与action方法的返回值
MySQL---基本的select语句
GP 6总体架构学习笔记
阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
21.支持向量机—核函数的介绍
你辛辛苦苦写的文章可能不是你的原创
MySQL - single function
多线程之锁
【Yugong Series】July 2022 Go Teaching Course 021-Slicing Operation of Go Containers