version: 1.10

package list

import "container/list"

概述

list 包实现一个双向链表。

迭代一个链表(这里 l 是一个 List 指针):

  1. for e := l.Front(); e != nil; e = e.Next() {
  2. // do something with e.Value
  3. }

例子:

  1. // 创建一个新的链表,然后放入若干数字。
  2. l := list.New()
  3. e4 := l.PushBack(4)
  4. e1 := l.PushFront(1)
  5. l.InsertBefore(3, e4)
  6. l.InsertAfter(2, e1)
  7. // 迭代链表并打印内容。
  8. for e := l.Front(); e != nil; e = e.Next() {
  9. fmt.Println(e.Value)
  10. }
  11. // Output:
  12. // 1
  13. // 2
  14. // 3
  15. // 4

索引

例子

文件

list.go

type Element

  1. type Element struct {
  2.  
  3. // 该元素中存储的值。
  4. Value interface{}
  5. // 包含被过滤的或未导出的字段
  6. }

Element 是链表里的一个元素。

func (*Element) Next

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

Next 方法返回链表里的下一个元素或 nil 。

func (*Element) Prev

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

Prev 方法返回链表里的前一个元素或 nil 。

type List

  1. type List struct {
  2. // contains filtered or unexported fields
  3. }

List 类型表示一个双向链表。List 的零值表示空链表。

func New

  1. func New() *List

New 函数初始化一个新链表。

func (*List) Back

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

Back 方法返回链表的最后一个元素,如果链表是空的则返回 nil 。

func (*List) Front

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

Front 方法返回链表的第一个元素,如果链表是空的则返回 nil 。

func (*List) Init

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

Init 方法初始化或清空链表 l 。

func (*List) InsertAfter

  1. func (l *List) InsertAfter(v interface{}, mark *Element) *Element

InsertAfter 方法在参数 mark 后面插入一个值为 v 的新元素 e 并返回 e 。如果参数 mark 不是 l 的元素,链表不会被修改。参数 mark 一定不能为 nil 。

func (*List) InsertBefore

  1. func (l *List) InsertBefore(v interface{}, mark *Element) *Element

InsertBefore 方法在参数 mark 前面插入一个值为 v 的新元素 e 并返回 e 。如果参数 mark 不是 l 的元素,链表不会被修改。参数 mark 一定不能为 nil 。

func (*List) Len

  1. func (l *List) Len() int

Len 方法返回链表 l 中元素的个数。算法的时间复杂度是 O(1)。

func (*List) MoveAfter

  1. func (l *List) MoveAfter(e, mark *Element)

MoveAfter 方法移动元素 e 到参数 mark 后面的新位置。如果 e 或 mark 不是链表 l 的元素,或 e 等于 mark,链表不会被修改。e 和 mark 一定不能为 nil 。

func (*List) MoveBefore

  1. func (l *List) MoveBefore(e, mark *Element)

MoveBefore 方法移动元素 e 到参数 mark 前面的新位置。如果 e 或 mark 不是链表 l 的元素,或 e 等于 mark,链表不会被修改。e 和 mark 一定不能为 nil 。

func (*List) MoveToBack

  1. func (l *List) MoveToBack(e *Element)

MoveToBack 方法移动元素 e 到链表的末尾。如果 e 不是链表 l 的元素,链表不会被修改。e 一定不能为 nil 。

func (*List) MoveToFront

  1. func (l *List) MoveToFront(e *Element)

MoveToFront 方法移动元素 e 到链表的开头。如果 e 不是链表 l 的元素,链表不会被修改。e 一定不能为 nil 。

func (*List) PushBack

  1. func (l *List) PushBack(v interface{}) *Element

PushBack 方法插入值为 v 的新元素 e 到链表 l 的末尾,并返回 e 。

func (*List) PushBackList

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

PushBackList 方法插入链表 other 的一份拷贝到链表 l 的末尾。链表 l 和 other 可能一样,但它们一定不能为 nil 。

func (*List) PushFront

  1. func (l *List) PushFront(v interface{}) *Element

PushFront 方法插入值为 v 的新元素 e 到链表 l 的开头,并返回 e 。

func (*List) PushFrontList

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

PushFrontList 方法插入链表 other 的一份拷贝到链表 l 的开头。链表 l 和 other 可能一样,但它们一定不能为 nil 。

func (*List) Remove

  1. func (l *List) Remove(e *Element) interface{}

Remove 方法从链表 l 中移除元素 e 。这个函数返回元素 e 的值,即 e.Value 。元素 e 一定不能为 nil 。