当前位置:网站首页>【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)}
边栏推荐
- Chinese encoding Settings and action methods return values
- 【AcWing】第 62 场周赛 【2022.07.30】
- Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
- Huawei mobile phone one-click to open "maintenance mode" to hide all data and make mobile phone privacy more secure
- Golang 必知必会Go Mod命令
- 多数据中心操作和检测并发写入
- Go basic part study notes
- ResNet的基础:残差块的原理
- JS基础小练习
- MySQL---sort and pagination
猜你喜欢
adb shell 报错error: device unauthorized
Introduction of Jerry voice chip ic toy chip ic_AD14NAD15N full series development
基于WPF重复造轮子,写一款数据库文档管理工具(一)
AcWing 1282. 搜索关键词 题解((AC自动机)Trie+KMP)+bfs)
手把手教你学会部署Nestjs项目
Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言
GateWay实现负载均衡
【网络通信三】研华网关Modbus服务设置
如何识别假爬虫?
MySQL - multi-table query
随机推荐
研发过程中的文档管理与工具
Bika LIMS open source LIMS set - use of SENAITE (detection process)
[Network Communication 3] Advantech Gateway Modbus Service Settings
如何识别假爬虫?
MySQL---排序与分页
华为手机一键开启“维修模式”隐藏所有数据,让手机隐私更加安全
无主复制系统(1)-节点故障时写DB
MySQL---聚合函数
All-platform GPU general AI video supplementary frame super-score tutorial
Flutter 获取状态栏statusbar的高度
Mariabackup implements incremental data backup for Mariadb 10.3
Intelligent bin (9) - vibration sensor (raspberries pie pico implementation)
C# 之 扑克游戏 -- 21点规则介绍和代码实现
ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!...
flyway的快速入门教程
【Yugong Series】July 2022 Go Teaching Course 022-Dictionary of Go Containers
抖音根据关键词取视频列表 API
adb shell error error: device unauthorized
Golang 切片删除指定元素的几种方法
After Effects tutorial, How to adjust overexposed snapshots in After Effects?