list

package module
v0.0.0-...-1f44755 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2023 License: MIT Imports: 4 Imported by: 1

README

Linked List Implemented in Go

Features
  • Dynamic singly-linked list with generic type
  • Supports append, get, set, insert, delete (by value), remove (by index), and indexOf methods
  • Append and insert are variadic (variable number of arguments)
  • Supports shuffle, bogo sort, copy, length, clear, sublist, and insertList methods
  • Merge sort in O(nlogn) and binary search in O(n)
  • Iterator for efficient traversal of list
  • Returns error when index out of bounds or invalid operation

See list-tester-golang for an example of how to import and use the linked list

Learn about Go with A Tour of Go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IndexError

type IndexError struct {
	// contains filtered or unexported fields
}

Index out of bounds error, Implements error interface

func (*IndexError) Error

func (e *IndexError) Error() string

Return error message for IndexError

type InvalidError

type InvalidError struct {
	// contains filtered or unexported fields
}

Invalid operation error, Implements error interface

func (*InvalidError) Error

func (e *InvalidError) Error() string

Return error message for InvalidError

type Iterator

type Iterator[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

Iterator for efficient traversal of linked list

func (*Iterator[T]) Add

func (iter *Iterator[T]) Add(v T)

Add element with iterator

func (*Iterator[T]) HasNext

func (iter *Iterator[T]) HasNext() bool

Return whether iterator has next element

func (*Iterator[T]) Next

func (iter *Iterator[T]) Next() (*List[T], error)

Return next element of iterator

func (*Iterator[T]) Remove

func (iter *Iterator[T]) Remove() error

Remove element last returned by iterator

type List

type List[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

List represents a singly-linked list that holds values of ordered type (string, int, float), Implements Stringer interface

func (*List[T]) Add

func (list *List[T]) Add(vs ...T)

Append values to the list

func (*List[T]) Bogo

func (list *List[T]) Bogo()

Bogo sort

func (*List[_]) Clear

func (list *List[_]) Clear()

Clear list

func (*List[T]) Copy

func (list *List[T]) Copy() List[T]

Copy list to a new list

func (*List[T]) Delete

func (list *List[T]) Delete(v T) bool

Delete the first occurence of v, Return whether deletion succeeded

func (*List[T]) Get

func (list *List[T]) Get(index int) (T, error)

Get value at index, Return error if index out of bounds

func (*List[T]) IndexOf

func (list *List[T]) IndexOf(v T) int

Return index of v, or -1 if not found

func (*List[T]) Insert

func (list *List[T]) Insert(index int, vs ...T) error

Insert values at index, Return error if index out of bounds

func (*List[T]) InsertList

func (list *List[T]) InsertList(index int, other *List[T]) error

Insert list at index

func (*List[_]) Length

func (list *List[_]) Length() int

Return length of list

func (*List[T]) Remove

func (list *List[T]) Remove(index int) (T, error)

Remove element at index, Return error if index out of bounds

func (*List[T]) Search

func (list *List[T]) Search(v T) int

Binary search for v (inefficient in linked list), List must be sorted, Return index, or (-insertion point-1) if not found

func (*List[T]) Set

func (list *List[T]) Set(index int, v T) (T, error)

Set index to v, Return error if index out of bounds

func (*List[T]) Shuffle

func (list *List[T]) Shuffle()

Fisher-Yates shuffle

func (*List[_]) Sort

func (list *List[_]) Sort()

Merge sort the list

func (*List[T]) Sorted

func (list *List[T]) Sorted() bool

Check if list sorted

func (List[_]) String

func (list List[_]) String() string

Convert list to a string

func (*List[T]) Sublist

func (list *List[T]) Sublist(index int) (*List[T], error)

Return sublist starting at index

Jump to

Keyboard shortcuts

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