要点
- 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.