list

原文:https://pkg.go.dev/container/list@go1.23.0

Package list implements a doubly linked list.

​ list包实现了一个双链表(a doubly linked list.)。

To iterate over a list (where l is a *List):

​ 迭代一个列表(其中l是一个*List):

1
2
3
4
for e := l.Front(); e != nil; e = e.Next() {
    // do something with e.Value
	//对e.Value做一些处理
}

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

常量

This section is empty.

变量

This section is empty.

函数

This section is empty.

类型

type Element

1
2
3
4
5
6
7
type Element struct {

	// The value stored with this element.
    // 这个元素存储的值。
	Value any
	// contains filtered or unexported fields
}

Element is an element of a linked list.

Element结构体是链表的一个元素。

(*Element) Next

1
func (e *Element) Next() *Element

Next returns the next list element or nil.

Next方法返回下一个列表元素或nil

(*Element) Prev

1
func (e *Element) Prev() *Element

Prev returns the previous list element or nil.

Prev方法返回前一个列表元素或nil

type List

1
2
3
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.

List结构体表示一个双链表。List的零值是一个准备使用的空列表。

func New

1
func New() *List

New returns an initialized list.

New函数返回一个初始化的列表。

(*List) Back

1
func (l *List) Back() *Element

Back returns the last element of list l or nil if the list is empty.

Back方法返回列表l的最后一个元素,如果列表为空,则返回nil

(*List) Front

1
func (l *List) Front() *Element

Front returns the first element of list l or nil if the list is empty.

Front方法返回列表l的第一个元素,如果列表为空则返回nil

(*List) Init

1
func (l *List) Init() *List

Init initializes or clears list l.

Init方法初始化或清除列表l

(*List) InsertAfter

1
func (l *List) InsertAfter(v any, 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. The mark must not be nil.

InsertAfter方法在mark之后插入一个新的元素e,其值为v,并返回e。如果mark不是l的一个元素,该列表不会被修改。mark不能是nil

(*List) InsertBefore

1
func (l *List) InsertBefore(v any, 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. The mark must not be nil.

InsertBefore方法在mark之前插入一个新的元素e,其值为v,并返回e,如果mark不是l的一个元素,该列表就不会被修改。mark不能是nil

(*List) Len

1
func (l *List) Len() int

Len returns the number of elements of list l. The complexity is O(1).

Len方法返回列表l的元素数,其复杂度为O(1)

(*List) MoveAfter <- go1.2

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. The element and mark must not be nil.

MoveAfter方法将元素e移动到mark之后的新位置。如果emark不是l的一个元素,或者e == mark,该列表不会被修改。emark元素不能是nil

(*List) MoveBefore <- go1.2

1
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. The element and mark must not be nil.

MoveBefore方法将元素e移动到mark之前的新位置。如果emark不是l的一个元素,或者e == mark,该列表不会被修改。emark元素不能是nil

(*List) MoveToBack

1
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. The element must not be nil.

MoveToBack方法把元素e移到列表l的后面。如果e不是l的一个元素,该列表不会被修改。该元素不能是nil

(*List) MoveToFront

1
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. The element must not be nil.

MoveToFront方法把元素e移到列表l的前面,如果e不是l的元素,该列表不会被修改。该元素不能是nil

(*List) PushBack

1
func (l *List) PushBack(v any) *Element

PushBack inserts a new element e with value v at the back of list l and returns e.

PushBack方法在列表l的后面插入一个新元素e,其值为v,并返回e

(*List) PushBackList

1
func (l *List) PushBackList(other *List)

PushBackList inserts a copy of another list at the back of list l. The lists l and other may be the same. They must not be nil.

PushBackList方法在列表l的后面插入一个另一个列表的副本。它们不能是nil

(*List) PushFront

1
func (l *List) PushFront(v any) *Element

PushFront inserts a new element e with value v at the front of list l and returns e.

PushFront方法在列表l的前面插入一个值为v的新元素e,并返回e

(*List) PushFrontList

1
func (l *List) PushFrontList(other *List)

PushFrontList inserts a copy of another list at the front of list l. The lists l and other may be the same. They must not be nil.

PushFrontList方法在列表l的前面插入一个另一个列表的副本。它们不能是nil

(*List) Remove

1
func (l *List) Remove(e *Element) any

Remove removes e from l if e is an element of list l. It returns the element value e.Value. The element must not be nil.

​ 如果e是列表l的一个元素,Remove方法将el中移除,并返回元素值e.Value。该元素不能是nil

最后修改 October 10, 2024: 更新 (a4b8f85)