Go-List
时间:2022-05-05
本文章向大家介绍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.
- RecyclerView notifyItem闪烁的问题
- 独家: iOS是如何收集用户的地理信息的
- Hygieia 为何物?DevOps 利器也
- 汽车攻击离你很近:一分钟变成汽车黑客
- LIFX智能灯泡漏洞泄露WIFI密码
- android ViewPager+Fragment之懒加载
- 逆向APK进行smali注入实现“秒破WIFI”
- 我所理解的Android 启动模式
- 搭建开源入侵检测系统Snort,并实现与防火墙联动
- 如何在Python中为长短期记忆网络扩展数据
- android 开发Handler源码剖析
- Keras作为TensorFlow的简化界面:教程
- Android基础:Fragment,看这篇就够了
- Rest api简介
- 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 数组属性和方法