当前位置:网站首页>【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)}
边栏推荐
- Automated testing - web automation - first acquaintance with selenium
- Flutter 获取状态栏statusbar的高度
- [pytorch] 1.7 pytorch and numpy, tensor and array conversion
- MySQL---Basic select statement
- Taobao/Tmall get Taobao password real url API
- 2022年Android 面经总结(附含面试题 | 源码 | 面试资料)
- [TypeScript]OOP
- flyway的快速入门教程
- Golang 小数操作之判断几位小数点与四舍五入
- Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
猜你喜欢
Automated testing - web automation - first acquaintance with selenium
Huawei mobile phone one-click to open "maintenance mode" to hide all data and make mobile phone privacy more secure
[pytorch] 1.7 pytorch and numpy, tensor and array conversion
研发过程中的文档管理与工具
TestCafe之如何进行调试
【码蹄集新手村600题】不通过字符数组来合并俩个数字
MySQL---多表查询
leetcode:6135. 图中的最长环【内向基环树 + 最长环板子 + 时间戳】
【pytorch】1.7 pytorch与numpy,tensor与array的转换
All-platform GPU general AI video supplementary frame super-score tutorial
随机推荐
使用 Flutter 和 Firebase 制作!计数器应用程序
Golang 小数操作之判断几位小数点与四舍五入
[pytorch] pytorch automatic derivation, Tensor and Autograd
【Yugong Series】July 2022 Go Teaching Course 020-Array of Go Containers
2022年Android 面经总结(附含面试题 | 源码 | 面试资料)
flutter设置statusbar状态栏的背景颜色和 APP(AppBar)内部颜色一致方法。
adb shell 报错error: device unauthorized
[Source code analysis] BeanFactory and FactoryBean
多数据中心操作和检测并发写入
【源码解析】BeanFactory和FactoryBean
MySQL---创建和管理数据库和数据表
Anaconda如何顺利安装CV2
全平台GPU通用AI视频补帧超分教程
MySQL---子查询
35 MySQL interview questions and diagrams, this is also easy to understand
MySQL---排序与分页
新型电信“套路”,我爸中招了!
C# 之 扑克游戏 -- 21点规则介绍和代码实现
【Yugong Series】July 2022 Go Teaching Course 022-Dictionary of Go Containers
Go record - slice