list

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2024 License: MIT Imports: 0 Imported by: 1

Documentation

Overview

Package list implements a doubly linked list.

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

for e := l.Front(); e != nil; e = e.Next() {
	// do something with e.Value
}
Example
package main

import (
	"fmt"
	"log"

	"github.com/smallnest/exp/container/list"
)

func main() {
	// Create a new list and put some numbers in it.
	l := list.New[int]()
	e4 := l.PushBack(4)
	log.Println("Pushed 4 to back")
	e1 := l.PushFront(1)
	log.Println("Pushed 1 to front")
	l.InsertBefore(3, e4)
	log.Println("Inserted 3 before 4")
	l.InsertAfter(2, e1)
	log.Println("Inserted 2 after 1")

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element[V any] struct {

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

Element is an element of a linked list.

func (*Element[V]) Next

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

Next returns the next list element or nil.

func (*Element[V]) Prev

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

Prev returns the previous list element or nil.

type List

type List[V any] struct {
	// contains filtered or unexported fields
}

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

func New

func New[V any]() *List[V]

New returns an initialized list.

func (*List[V]) Back

func (l *List[V]) Back() *Element[V]

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

func (*List[V]) Front

func (l *List[V]) Front() *Element[V]

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

func (*List[V]) Init

func (l *List[V]) Init() *List[V]

Init initializes or clears list l.

func (*List[V]) InsertAfter

func (l *List[V]) InsertAfter(v V, mark *Element[V]) *Element[V]

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[V]) InsertBefore

func (l *List[V]) InsertBefore(v V, mark *Element[V]) *Element[V]

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[V]) Len

func (l *List[V]) Len() int

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

func (*List[V]) MoveAfter

func (l *List[V]) MoveAfter(e, mark *Element[V])

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[V]) MoveBefore

func (l *List[V]) MoveBefore(e, mark *Element[V])

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[V]) MoveToBack

func (l *List[V]) MoveToBack(e *Element[V])

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[V]) MoveToFront

func (l *List[V]) MoveToFront(e *Element[V])

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[V]) PushBack

func (l *List[V]) PushBack(v V) *Element[V]

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

func (*List[V]) PushBackList

func (l *List[V]) PushBackList(other *List[V])

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.

func (*List[V]) PushFront

func (l *List[V]) PushFront(v V) *Element[V]

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

func (*List[V]) PushFrontList

func (l *List[V]) PushFrontList(other *List[V])

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.

func (*List[V]) Remove

func (l *List[V]) Remove(e *Element[V]) V

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.

Jump to

Keyboard shortcuts

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