version: 1.10

package list

import "container/list"

Overview

Package list implements a doubly linked list.

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

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

Example:

  1. // Create a new list and put some numbers in it.
  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. // Iterate through list and print its contents.
  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

Index

Examples

Package files

list.go

type Element

  1. type Element struct {
  2.  
  3. // The value stored with this element.
  4. Value interface{}
  5. // contains filtered or unexported fields
  6. }

Element is an element of a linked list.

func (*Element) Next

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

Next returns the next list element or nil.

func (*Element) Prev

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

Prev returns the previous list element or nil.

type List

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

List represents a doubly linked list. The zero value for List is an empty list ready to use.

func New

  1. func New() *List

New returns an initialized list.

func (*List) Back

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

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

func (*List) Front

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

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

func (*List) Init

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

Init initializes or clears list l.

func (*List) InsertAfter

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

func (*List) InsertBefore

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

func (*List) Len

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

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

func (*List) MoveAfter

  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.

func (*List) MoveBefore

  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.

func (*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.

func (*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.

func (*List) PushBack

  1. 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 (*List) PushBackList

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

func (*List) PushFront

  1. 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 (*List) PushFrontList

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

func (*List) Remove

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