Go-List
时间:2022-05-06
本文章向大家介绍Go-List,主要内容包括要点、示例、list-Elment的数据类型、godoc list、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
要点
- Element表示链表的一个元素,List表示链表
- 访问元素的值: .Value
- list是双向链表,可以在指定的位置插入元素
- 初始化: New()。——和var x List 是等价的,但New()可读性更好。
- 元素个数: Len()
- 遍历: (1) Front()和Back()分别取链表的第一个元素和最后一个元素。如果为空,则返回nil。(2) Next()和Prev()分别返回当前元素的写一个或前一个元素。如果为空,则返回nil。 (3) 遍历整个链表的通用方法就是先Front(),然后依次Next()。
- 插入: InsertAfter(), InsertBefore(), PushBack(), PushBackList(), PushFront(), PushFrontList(). ——xxxList()是把另外一个链表的原始添加到当前链表上。
- 移动元素: MoveAfter(), MoveBefore(), MoveToBack(), MoveToFront()
- 删除元素: Remove()
注: - 之前在讲述struct的时候,并没有特别提到类函数或对象函数,或者类方法或对象方法。这里的New()即为类函数,或者说普通的函数。——也可以认为在Go中没有类函数的概念,说成package function可能更合适。因为在调用的时候,需要package_name.FunctionName()。 - 几乎list所有的成员方法都返回*Element类型,而这个返回值又可以作为其他成员方法的入参。 - 建议阅读list的源码,以了解进一步的详细信息。
示例
以src/Container/list/example_est.go为素材。
package main
import (
"fmt"
"container/list"
)
/*
D:examples>go run helloworld.go
0 1 2 3 4 5
D:examples>
*/
func main() {
var x list.List // or "x := list.New()"
e1 := x.PushBack(1)
x.PushBack(3)
e5 := x.PushBack(5)
x.PushFront(0)
x.InsertAfter(2, e1)
x.InsertBefore(4, e5)
for e := x.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value.(int), "t")
}
}
list-Elment的数据类型
前面提到了Element的Value数据成员或Value field. 注意到示例代码中的fmt.Print()一行。另注意到如下定义:
// Element is an element of a linked list.
type Element struct {
// ...
// The value stored with this element.
Value interface{}
}
即这里的Value可以是任意的数据类型,包括Go的基本数据类型,以及用户自定义的数据类型。
进一步地,我们可以预期:一个List中可以放各种不同的数据类型。为此,给出如下示例予以说明。
package main
import (
"fmt"
"container/list"
)
type Point struct {
x, y int
}
/*
D:examples>go run helloworld.go
1 2 3 Four Five {6 66} {7 77}
D:examples>
*/
func main() {
var x list.List
x.PushBack(1)
x.PushBack(2)
x.PushBack(3)
x.PushBack("Four")
x.PushBack("Five")
x.PushBack(Point{6, 66})
x.PushBack(Point{7, 77})
for e := x.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, "t")
}
}
特别注意到ftm.Print()和之前示例的区别。——请参考Go-Errors一文中的“Println()的入参”一节,以初步了解Print()的处理流程。
godoc list
执行命令:godoc cmd/container/list
PACKAGE DOCUMENTATION
package list
import "."
Package list implements a doubly linked list.
To iterate over a list (where l is a *List):
for e := l.Front(); e != nil; e = e.Next() {
// do something with e.Value
}
TYPES
type Element struct {
// The value stored with this element.
Value interface{}
// contains filtered or unexported fields
}
Element is an element of a linked list.
func (e *Element) Next() *Element
Next returns the next list element or nil.
func (e *Element) Prev() *Element
Prev returns the previous list element or nil.
type List struct {
// contains filtered or unexported fields
}
List represents a doubly linked list. The zero value for List is an
empty list ready to use.
func New() *List
New returns an initialized list.
func (l *List) Back() *Element
Back returns the last element of list l or nil.
func (l *List) Front() *Element
Front returns the first element of list l or nil.
func (l *List) Init() *List
Init initializes or clears list l.
func (l *List) InsertAfter(v interface{}, mark *Element) *Element
InsertAfter inserts a new element e with value v immediately after mark
and returns e. If mark is not an element of l, the list is not modified.
func (l *List) InsertBefore(v interface{}, mark *Element) *Element
InsertBefore inserts a new element e with value v immediately before
mark and returns e. If mark is not an element of l, the list is not
modified.
func (l *List) Len() int
Len returns the number of elements of list l. The complexity is O(1).
func (l *List) MoveAfter(e, mark *Element)
MoveAfter moves element e to its new position after mark. If e or mark
is not an element of l, or e == mark, the list is not modified.
func (l *List) MoveBefore(e, mark *Element)
MoveBefore moves element e to its new position before mark. If e or mark
is not an element of l, or e == mark, the list is not modified.
func (l *List) MoveToBack(e *Element)
MoveToBack moves element e to the back of list l. If e is not an element
of l, the list is not modified.
func (l *List) MoveToFront(e *Element)
MoveToFront moves element e to the front of list l. If e is not an
element of l, the list is not modified.
func (l *List) PushBack(v interface{}) *Element
PushBack inserts a new element e with value v at the back of list l and
returns e.
func (l *List) PushBackList(other *List)
PushBackList inserts a copy of an other list at the back of list l. The
lists l and other may be the same.
func (l *List) PushFront(v interface{}) *Element
PushFront inserts a new element e with value v at the front of list l
and returns e.
func (l *List) PushFrontList(other *List)
PushFrontList inserts a copy of an other list at the front of list l.
The lists l and other may be the same.
func (l *List) Remove(e *Element) interface{}
Remove removes e from l if e is an element of list l. It returns the
element value e.Value.
- linux 添加用户到sudo中
- 机器学习之白话adaboost元算法
- 查看占用内存多的进程
- jboss EAP 6.2 + Message Drive Bean(MDB) 整合IBM Webshpere MQ 7.5
- 通过jenkins API去build一个job
- Django---分页器、中间件
- 启动jenkins服务错误
- 如果未来的AI拥有意识,你舍得不理它吗?
- centos下安装python3
- jboss:在standalone.xml中设置系统属性(system-properties)
- iptables
- Django-form表单
- 比较git commit 两个版本之间次数
- eclipse: workspace出错导致无法启用的解决
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 持续代码质量管理-SonarQube Scanner部署 2.1. 软件安装2.2. 配置修改
- 《前端那些事》聊聊前端的按需加载
- 直播带货系统,滚动视图,上滑隐藏,下滑显示
- 持续代码质量管理-SonarQube-7.3简单使用 2.1. 查看配置2.2. 质量检测2.3. 浏览器查看
- 安装指定版本的docker服务
- 你学BFF和Serverless了吗
- 如何使用Java连接Kerberos的Phoenix
- docker swarm的常用操作
- 组件库源码中这些写法你掌握了吗?
- spark-2.4.0-hadoop2.7-安装部署 4.1. Spark安装4.2. 环境变量修改4.3. 配置修改4.4. 分发到其他机器4.5. 启动spark
- spark-2.4.0-hadoop2.7-高可用(HA)安装部署 5.1. Spark安装5.2. 环境变量修改5.3. 配置修改5.4. 分发到其他机器5.5.
- spark-2.4.0-hadoop2.7-简单操作 2.1. 相关截图
- Navicat Premium 12.0.24安装与激活(亲测已成功激活) 2.1. 下载激活文件2.2. 激活步骤准备工作2.3. 激活Navicat
- VMware实现iptables NAT及端口映射
- Saltstack_使用指南01_部署