Documentation
¶
Overview ¶
singly implements a generic singly-linked-list library for Go.
Index ¶
- Variables
- type LinkedList
- func (s *LinkedList[T]) Add(data T, index int) error
- func (s *LinkedList[T]) AddFirst(data T)
- func (s *LinkedList[T]) AddLast(data T)
- func (s *LinkedList[T]) First() (data T, ok bool)
- func (s *LinkedList[T]) IsEmpty() bool
- func (s *LinkedList[T]) Last() (data T, ok bool)
- func (s *LinkedList[T]) Remove(index int) (val T, err error)
- func (s *LinkedList[T]) RemoveFirst() (val T, ok bool)
- func (s *LinkedList[T]) RemoveLast() (val T, ok bool)
- func (s *LinkedList[T]) String() string
- func (s *LinkedList[T]) ToSlice() []T
- type Node
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidIndex = errors.New("invalid index") ErrEmptyList = errors.New("list is empty") )
Functions ¶
This section is empty.
Types ¶
type LinkedList ¶
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)