当前位置:网站首页>【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.List4.列表的操作
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)}
边栏推荐
- MySQL---聚合函数
- flutter设置statusbar状态栏的背景颜色和 APP(AppBar)内部颜色一致方法。
- Write a database document management tool based on WPF repeating the wheel (1)
- 华为手机一键开启“维修模式”隐藏所有数据,让手机隐私更加安全
- 无主复制系统(3)-Quorum一致性的局限性
- Intelligent bin (9) - vibration sensor (raspberries pie pico implementation)
- 【luogu P8326】Fliper(图论)(构造)(欧拉回路)
- [Network Communication 3] Advantech Gateway Modbus Service Settings
- 【Yugong Series】July 2022 Go Teaching Course 020-Array of Go Containers
- 35 MySQL interview questions and diagrams, this is also easy to understand
猜你喜欢
随机推荐
Huawei mobile phone one-click to open "maintenance mode" to hide all data and make mobile phone privacy more secure
The article you worked so hard to write may not be your original
【Yugong Series】July 2022 Go Teaching Course 022-Dictionary of Go Containers
Concurrency, Timing and Relativity
flutter设置statusbar状态栏的背景颜色和 APP(AppBar)内部颜色一致方法。
Combinatorics Notes (6) Associative Algebra of Locally Finite Partially Ordered Sets, Möbius Inversion Formula
学生管理系统第一天:完成登录退出操作逻辑 PyQt5 + MySQL5.8
go基础部分学习笔记记录
Masterless replication system (1) - write DB when node fails
程序员如何学习开源项目,这篇文章告诉你
flowable工作流所有业务概念
Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
MySQL---operator
你辛辛苦苦写的文章可能不是你的原创
TestCafe之如何进行调试
手把手教你学会部署Nestjs项目
几款永久免费内网穿透,好用且简单(内网穿透教程)
Apache EventMesh 分布式事件驱动多运行时
[Source code analysis] BeanFactory and FactoryBean
Write a database document management tool based on WPF repeating the wheel (1)









