singly

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2022 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

singly implements a generic singly-linked-list library for Go.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidIndex = errors.New("invalid index")
	ErrEmptyList    = errors.New("list is empty")
)

Functions

This section is empty.

Types

type LinkedList

type LinkedList[T any] struct {
	Head *Node[T]
	Tail *Node[T]
	Size int
}

func New

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

New constructs and returns an empty singly linked-list. time-complexity: O(1)

func (*LinkedList[T]) Add

func (s *LinkedList[T]) Add(data T, index int) error

Add adds a new node to the given index in the list. It returns ErrInvalidIndex if the given index is out of bound. time-complexity: O(n)

func (*LinkedList[T]) AddFirst

func (s *LinkedList[T]) AddFirst(data T)

AddFirst adds a new node to the beginning of the list. time-complexity: O(1)

func (*LinkedList[T]) AddLast

func (s *LinkedList[T]) AddLast(data T)

AddLast adds a new node to the end of the list. time-complexity: O(1)

func (*LinkedList[T]) First

func (s *LinkedList[T]) First() (data T, ok bool)

First returns the first element of the list. It returns false if the list is empty. time-complexity: O(1)

func (*LinkedList[T]) IsEmpty

func (s *LinkedList[T]) IsEmpty() bool

IsEmpty returns true if the linked-list doesn't contain any nodes. time-complexity: O(1)

func (*LinkedList[T]) Last

func (s *LinkedList[T]) Last() (data T, ok bool)

Last returns the last element of the list. It returns false if the list is empty. time-complexity: O(1)

func (*LinkedList[T]) Remove

func (s *LinkedList[T]) Remove(index int) (val T, err error)

Remove removes and returns the node in the given index. It returns ErrInvalidIndex and ErrEmptyList if the list is empty or the index is out of bound. time-complexity: O(n)

func (*LinkedList[T]) RemoveFirst

func (s *LinkedList[T]) RemoveFirst() (val T, ok bool)

RemoveFirst removes and returns the first element of the list. It returns false if the list is empty. time-complexity: O(1)

func (*LinkedList[T]) RemoveLast

func (s *LinkedList[T]) RemoveLast() (val T, ok bool)

RemoveLast removes and returns the last element of the list. It returns false if the list empty. time-complexity: O(n)

func (*LinkedList[T]) String

func (s *LinkedList[T]) String() string

String returns the string representation of the list. time-complexity: O(n)

func (*LinkedList[T]) ToSlice

func (s *LinkedList[T]) ToSlice() []T

ToSlice returns the linked-list as a slice. time-complexity: O(n)

type Node

type Node[T any] struct {
	Data T
	Next *Node[T]
}

func (*Node[T]) String

func (n *Node[T]) String() string

String returns the string representation of the node's data. time-complexity: O(1)

Jump to

Keyboard shortcuts

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