list

package
v0.4.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package list implements a doubly linked list.

Example
l := list.New[any]()

e3 := l.PushFront(3)
e4 := l.PushBack(4)
l.InsertAfter(5, e4)
l.InsertBefore(2, e3)

l2 := list.New[any]()
l2.PushBack(1)

l3 := list.New[any]()
l3.PushFront(6)

l.PushBackList(l2)
l.PushFrontList(l3)

head, tail := l.Front(), l.Back()
l.MoveToFront(tail)
l.MoveToBack(head)

// 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
5
6

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element[T any] struct {

	// The value stored with this element.
	Value T
	// contains filtered or unexported fields
}

Element is an element of a linked list.

func (*Element[T]) Next

func (e *Element[T]) Next() *Element[T]

Next returns the next list element or nil.

func (*Element[T]) Prev

func (e *Element[T]) Prev() *Element[T]

Prev returns the previous list element or nil.

type List

type List[T any] interface {
	// Clear release all element from List.
	Clear() List[T]
	// Len returns the number of elements of List.
	// The complexity is O(1).
	Len() int
	// Front returns the first element of List or nil if the List is empty.
	Front() *Element[T]
	// Back returns the last element of List or nil if the List is empty.
	Back() *Element[T]
	// Remove removes e from List if e is an element of List.
	// It returns the element value e.Value.
	// The element must not be nil.
	Remove(e *Element[T]) T
	// PushFront inserts a new element e with value v at the front of List
	// and returns Element of v.
	PushFront(v T) *Element[T]
	// PushBack inserts a new element e with value v at the back of List
	// and returns Element of v.
	PushBack(v T) *Element[T]
	// InsertBefore inserts a new element e with value v immediately before
	// mark and returns Element of v. If mark is not an element of List,
	// the List is not modified. The mark must not be nil.
	InsertBefore(v T, mark *Element[T]) *Element[T]
	// InsertAfter inserts a new element e with value v immediately after
	// mark and returns Element of v. If mark is not an element of List,
	// the list is not modified. The mark must not be nil.
	InsertAfter(v T, mark *Element[T]) *Element[T]
	// MoveToFront moves element e to the front of List.
	// If e is not an element of l, the List is not modified.
	// The element must not be nil.
	MoveToFront(e *Element[T])
	// MoveToBack moves element e to the back of List.
	// If e is not an element of l, the List is not modified.
	// The element must not be nil.
	MoveToBack(e *Element[T])
	// 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 *Element[T], mark *Element[T])
	// 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 *Element[T], mark *Element[T])
	// PushBackList inserts a copy of other List at the back of List.
	// The lists l and other may be the same. They must not be nil.
	PushBackList(other List[T])
	// PushFrontList inserts a copy of other List at the front of List.
	// The lists l and other may be the same. They must not be nil.
	PushFrontList(other List[T])
}

func New

func New[T any]() List[T]

New returns an initialized list.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL