linkedlist

package module
v0.0.3-0...-d9417b9 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2025 License: MIT Imports: 3 Imported by: 0

README

Lily

Generic Linked List with comparable types to be used as keys.

func main() {
	ll := lily.NewLinkedListInt()
	ll.Insert(0, []byte("rick"))
	ll.Insert(1, []byte("morty"))
	ll.Insert(2, []byte("summer"))
	ll.Insert(3, []byte("beth"))
	ll.Insert(4, []byte("jerry"))

	value, err := ll.Search(0)
	if err != nil {
		if errors.Is(err, lily.ErrNotFound) {
			// record not found
		} else if errors.Is(err, lily.ErrNilHead) {
			// uninitialized linked list
		}
	}

 	fmt.Println(string(value)) // "rick"
}

Benchmarks

builtin golang benchmark results

goos: linux
goarch: amd64
pkg: github.com/structx/lily
cpu: 13th Gen Intel(R) Core(TM) i5-1340P
BenchmarkInsert-16    	 6866752	       152.2 ns/op	      64 B/op	       2 allocs/op
PASS
ok  	github.com/structx/lily	1.239s


goos: linux
goarch: amd64
pkg: github.com/structx/lili
cpu: 13th Gen Intel(R) Core(TM) i5-1340P
=== RUN   BenchmarkDoubleLinkedListInsert
BenchmarkDoubleLinkedListInsert
BenchmarkDoubleLinkedListInsert-16         48422            160576 ns/op              64 B/op          2 allocs/op
PASS
ok      github.com/structx/lili 8.038s

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNilRoot
	ErrNilRoot = errors.New("nil root node")
	// ErrNotFound
	ErrNotFound = errors.New("key not found")
)

Functions

This section is empty.

Types

type DNode

type DNode[K comparable, T any] struct {
	// contains filtered or unexported fields
}

DNode

type DoubleLinkedList

type DoubleLinkedList[K comparable, T any] struct {
	// contains filtered or unexported fields
}

DoubleLinkedList

func (*DoubleLinkedList[K, T]) Flush

func (dll *DoubleLinkedList[K, T]) Flush()

Flush

func (*DoubleLinkedList[K, T]) Insert

func (dll *DoubleLinkedList[K, T]) Insert(key K, payload T) *DNode[K, T]

Insert

func (*DoubleLinkedList[K, T]) Search

func (dll *DoubleLinkedList[K, T]) Search(key K) (T, error)

Search

func (*DoubleLinkedList[K, T]) Size

func (dll *DoubleLinkedList[K, T]) Size() uintptr

Size

type Iterator

type Iterator[K comparable, T any] struct {
	// contains filtered or unexported fields
}

Iterator

func (*Iterator[K, T]) HasNext

func (it *Iterator[K, T]) HasNext() bool

HasNext returns conditional if more nodes

func (*Iterator[K, T]) Next

func (it *Iterator[K, T]) Next() any

Next returns next node payload

type LinkedList

type LinkedList[K comparable, T any] struct {
	// contains filtered or unexported fields
}

LinkedList generic linked list with comparable types

func (*LinkedList[K, T]) Flush

func (ll *LinkedList[K, T]) Flush()

Flush nil root and reset size

func (*LinkedList[K, T]) Insert

func (ll *LinkedList[K, T]) Insert(key K, payload T) *Node[K, T]

Insert new node into list

func (*LinkedList[K, T]) Iterator

func (ll *LinkedList[K, T]) Iterator() *Iterator[K, T]

Iterator

func (*LinkedList[K, T]) Search

func (ll *LinkedList[K, T]) Search(key K) (any, error)

Search list for key and return payload

func (*LinkedList[K, T]) Size

func (ll *LinkedList[K, T]) Size() uintptr

Size returns size of list

type Node

type Node[K comparable, T any] struct {
	Key     K
	Payload T
	// contains filtered or unexported fields
}

Node

Jump to

Keyboard shortcuts

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