package list
import "container/list"
Package list 实现了双向链表。
遍历链表的代码(其中 l 是 *List):
for e := l.Front(); e != nil; e = e.Next() {
// 对 e.Value 做某些操作
}
Example
package main import ( "container/list" "fmt" ) func main() { // Create a new list and put some numbers in it. l := list.New() e4 := l.PushBack(4) e1 := l.PushFront(1) l.InsertBefore(3, e4) l.InsertAfter(2, e1) // Iterate through list and print its contents. for e := l.Front(); e != nil; e = e.Next() { fmt.Println(e.Value) } }
Output:
1 2 3 4
Index
- type Element
-
type List
- func New() *List
- func (l *List) Back() *Element
- func (l *List) Front() *Element
- func (l *List) Init() *List
- func (l *List) InsertAfter(v any, mark *Element) *Element
- func (l *List) InsertBefore(v any, mark *Element) *Element
- func (l *List) Len() int
- func (l *List) MoveAfter(e, mark *Element)
- func (l *List) MoveBefore(e, mark *Element)
- func (l *List) MoveToBack(e *Element)
- func (l *List) MoveToFront(e *Element)
- func (l *List) PushBack(v any) *Element
- func (l *List) PushBackList(other *List)
- func (l *List) PushFront(v any) *Element
- func (l *List) PushFrontList(other *List)
- func (l *List) Remove(e *Element) any
Examples
Types
type Element
type Element struct { // 存储在该元素中的值。 Value any // contains filtered or unexported fields }
Element 是链表中的元素。
func (*Element) Next
func (e *Element) Next() *Element
Next 返回下一个链表元素;如果不存在则返回 nil。
func (*Element) Prev
func (e *Element) Prev() *Element
Prev 返回前一个链表元素;如果不存在则返回 nil。
type List
type List struct { // contains filtered or unexported fields }
List 表示一个双向链表。 List 的零值是一个空的、即可使用的链表。
func New
func New() *List
New 返回一个已初始化的链表。
func (*List) Back
func (l *List) Back() *Element
Back 返回链表 l 的最后一个元素;如果链表为空则返回 nil。
func (*List) Front
func (l *List) Front() *Element
Front 返回链表 l 的第一个元素;如果链表为空则返回 nil。
func (*List) Init
func (l *List) Init() *List
Init 初始化或清空链表 l。
func (*List) InsertAfter
func (l *List) InsertAfter(v any, mark *Element) *Element
InsertAfter 在 mark 之后立即插入一个值为 v 的新元素 e,并返回 e。 如果 mark 不是 l 的元素,链表不会被修改。 mark 不能为 nil。
func (*List) InsertBefore
func (l *List) InsertBefore(v any, mark *Element) *Element
InsertBefore 在 mark 之前立即插入一个值为 v 的新元素 e,并返回 e。 如果 mark 不是 l 的元素,链表不会被修改。 mark 不能为 nil。
func (*List) Len
func (l *List) Len() int
Len 返回链表 l 中元素的数量。 时间复杂度为 O(1)。
func (*List) MoveAfter
func (l *List) MoveAfter(e, mark *Element)
MoveAfter 将元素 e 移动到 mark 之后的新位置。 如果 e 或 mark 不是 l 的元素,或者 e == mark,链表不会被修改。 e 和 mark 不能为 nil。
func (*List) MoveBefore
func (l *List) MoveBefore(e, mark *Element)
MoveBefore 将元素 e 移动到 mark 之前的新位置。 如果 e 或 mark 不是 l 的元素,或者 e == mark,链表不会被修改。 e 和 mark 不能为 nil。
func (*List) MoveToBack
func (l *List) MoveToBack(e *Element)
MoveToBack 将元素 e 移动到链表 l 的末尾。 如果 e 不是 l 的元素,链表不会被修改。 e 不能为 nil。
func (*List) MoveToFront
func (l *List) MoveToFront(e *Element)
MoveToFront 将元素 e 移动到链表 l 的前端。 如果 e 不是 l 的元素,链表不会被修改。 e 不能为 nil。
func (*List) PushBack
func (l *List) PushBack(v any) *Element
PushBack 在链表 l 的末尾插入一个值为 v 的新元素 e,并返回 e。
func (*List) PushBackList
func (l *List) PushBackList(other *List)
PushBackList 将另一个链表的副本插入到链表 l 的末尾。 l 和 other 可以是同一个链表。other 不能为 nil。
func (*List) PushFront
func (l *List) PushFront(v any) *Element
PushFront 在链表 l 的前端插入一个值为 v 的新元素 e,并返回 e。
func (*List) PushFrontList
func (l *List) PushFrontList(other *List)
PushFrontList 将另一个链表的副本插入到链表 l 的前端。 l 和 other 可以是同一个链表。other 不能为 nil。
func (*List) Remove
func (l *List) Remove(e *Element) any
Remove 从链表 l 中移除元素 e(如果 e 是链表 l 的元素)。 返回元素的值 e.Value。 e 不能为 nil。