当前位置:网站首页>【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
2022-08-01 03:37:00 【愚公搬代码】
文章目录
一、Go容器之列表
1.列表的定义
列表是一种数据结构,由多元素成的有限序列,即按照一定的线性顺序排列而成的数据项的集合,在这种数据结构上可以进行元素的的插入,删除,修改,和查找。
列表的两种主要表现是数组和链表,栈和队列是两种特殊类型的列表。列表(list)底层常见的数据结构有: 单链表、双链表等。
2.Go中的列表
在 Go 语言中,列表的实现都在 container/list 包中,内部实现原理是双链表。
列表(list)能够方便高效地进行元素的删除、插入操作。
3.列表的声明
变量名 := list.New()
//或
var 变量名 = list.List
4.列表的操作
4.1 列表添加元素
列表添加元素如下所示:
| 方法 | 功能 |
|---|---|
| InsertAfter(v interface{}, mark *Element) *Element | 在 mark 点后面插入元素 |
| InsertBefore(v interface{}, mark *Element) *Element | 在 mark 点前面插入元素 |
| PushFrontList(other *List) | 添加 other 列表中的元素到头部 |
| PushBackList(other *List) | 添加 other 列表中的元素到尾部 |
相关案例:
package main
import (
"container/list"
"fmt"
)
func main() {
l := list.New()
l.PushFront("头部愚公")
l.PushBack("尾部愚公")
// 遍历
for i := l.Front(); i != nil; i = i.Next() {
fmt.Println(i.Value)
}
}

其中 i := l.Front() 表示初始赋值,用来获取列表的头部下标;
然后每次会循环会判断 i != nil,若等于空,则会退出循环,否则执行 i.Next()继续循环下一个元素;
4.1 列表删除元素
package main
import (
"container/list"
"fmt"
)
func main() {
l := list.New()
// 头部添加字符串
l.PushFront("愚公1号")
// 尾部添加字符串
l.PushBack("愚公2号")
// 尾部添加一个整型,并保持元素句柄
element := l.PushBack(1)
// 在 1 之后添加字符串 2
l.InsertAfter("2", element)
// 在 1 之前添加字符串 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 获取列表头结点
package main
import (
"container/list"
"fmt"
)
func main() {
l := list.New()
// 头部添加字符串
l.PushFront("愚公1号")
// 尾部添加字符串
l.PushBack("愚公2号")
// 尾部添加一个整型,并保持元素句柄
element := l.PushBack(1)
// 在 1 之后添加字符串 2
l.InsertAfter("2", element)
fmt.Println("Front =", l.Front().Value)
}

4.3.2 获取列表尾结点
package main
import (
"container/list"
"fmt"
)
func main() {
l := list.New()
// 头部添加字符串
l.PushFront("愚公1号")
// 尾部添加字符串
l.PushBack("愚公2号")
// 尾部添加一个整型,并保持元素句柄
element := l.PushBack(1)
// 在 1 之后添加字符串 2
l.InsertAfter("2", element)
fmt.Println("Front =", l.Back().Value)
}

4.3.3 获取上一个结点
package main
import (
"container/list"
"fmt"
)
func main() {
//使用列表内置的 Prev() 函数,获取列表的上一个结点
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 main
import (
"container/list"
"fmt"
)
func main() {
//使用列表内置的 Prev() 函数,获取列表的上一个结点
listHaiCoder := list.New()
listHaiCoder.PushFront("1")
element := listHaiCoder.PushFront("2") //定义节点位置
listHaiCoder.PushFront("3")
preElement := element.Next()
fmt.Println("preElement =", preElement.Value)
}

边栏推荐
- The maximum quantity leetcode6133. Grouping (medium)
- Flutter Tutorial 02 Introduction to Flutter Desktop Program Development Tutorial Run hello world (tutorial includes source code)
- The kernel of the decompression process steps
- 2022 CSP-J1 CSP-S1 Round 1 Preliminary Competition Registration Guide
- What is dynamic programming and what is the knapsack problem
- 智芯传感输液泵压力传感器 为精准智能控制注入科技“强心剂”
- MySQL修改SQL语句优化性能
- second uncle
- Make your Lottie support word wrapping in text fields
- Difference Between Compiled and Interpreted Languages
猜你喜欢

IDEA does not recognize the module (there is no blue square in the lower right corner of the module)

Ordinary users cannot access HGFS directory

让你的 Lottie 支持文字区域内自动换行

The fledgling Xiao Li's 113th blog project notes: Wisdom cloud smart flower watering device combat (2) - basic Demo implementation

情人节浪漫3D照片墙【附源码】

【kali-信息收集】枚举——DNS枚举:DNSenum、fierce

如何下载Keil包

使用ts-node报错

Elastic Stack的介绍

IDEA debugging
随机推荐
【kali-信息收集】枚举——DNS枚举:DNSenum、fierce
HCIP(15)
Valentine's Day Romantic 3D Photo Wall [with source code]
/etc/fstab
Message queue MySQL table for storing message data
IDEA modifies the annotation font
"Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up
Which interpolation is better for opencv to zoom in and out??
IDEA does not recognize the module (there is no blue square in the lower right corner of the module)
ARM cross compilation
Simple vim configuration
Replacing the Raspberry Pi Kernel
移动端页面秒开优化总结
在打开MYSQL表时,有的可以显示编辑,有的没有,如何设置。
leetcode6132. Make all elements in an array equal to zero (simple, weekly)
The fledgling Xiao Li's 113th blog project notes: Wisdom cloud smart flower watering device combat (2) - basic Demo implementation
2. # code comments
HCIP (14)
智芯传感输液泵压力传感器 为精准智能控制注入科技“强心剂”
开源项目站点必备&交流区功能