Documentation
¶
Overview ¶
Package linkedlist provides a doubly linked list implementation.
Index ¶
- type LinkedList
- func (l *LinkedList[T]) Append(element T) *LinkedList[T]
- func (l *LinkedList[T]) AppendLeft(element T) *LinkedList[T]
- func (l LinkedList[T]) At(index int) (value T, err error)
- func (l *LinkedList[T]) Clear() *LinkedList[T]
- func (l LinkedList[T]) Concat(another LinkedList[T]) LinkedList[T]
- func (l LinkedList[T]) Copy() LinkedList[T]
- func (l LinkedList[T]) Count(element T) (count int)
- func (l LinkedList[T]) Empty() bool
- func (l LinkedList[T]) Equal(another LinkedList[T]) bool
- func (l LinkedList[T]) Every(condition func(T) bool) bool
- func (l *LinkedList[T]) Extend(another *LinkedList[T]) *LinkedList[T]
- func (l *LinkedList[T]) ExtendLeft(another *LinkedList[T]) *LinkedList[T]
- func (l *LinkedList[T]) Fill(element T, area ...int) *LinkedList[T]
- func (l LinkedList[T]) Filter(condition func(T) bool) LinkedList[T]
- func (l LinkedList[T]) Find(by func(T) bool) (element T, found bool)
- func (l LinkedList[T]) FindIndex(by func(T) bool) (index int)
- func (l LinkedList[T]) FindIndexes(by func(T) bool, counts ...int) (indexes []int)
- func (l LinkedList[T]) FindLast(by func(T) bool) (element T, found bool)
- func (l LinkedList[T]) FindLastIndex(by func(T) bool) (index int)
- func (l LinkedList[T]) FindLastIndexes(by func(T) bool, counts ...int) (indexes []int)
- func (l LinkedList[T]) FindLasts(by func(T) bool, counts ...int) (elements []T)
- func (l LinkedList[T]) Finds(by func(T) bool, counts ...int) (elements []T)
- func (l *LinkedList[T]) ForEach(action func(T) T) *LinkedList[T]
- func (l LinkedList[T]) Head() (element T, err error)
- func (l LinkedList[T]) Includes(element T) bool
- func (l LinkedList[T]) IndexOf(element T) (index int)
- func (l *LinkedList[T]) Insert(index int, element T) *LinkedList[T]
- func (l LinkedList[T]) LastIndexOf(element T) (index int)
- func (l LinkedList[T]) Len() int
- func (l LinkedList[T]) Map(handler func(T) T) LinkedList[T]
- func (l LinkedList[T]) MarshalJSON() ([]byte, error)
- func (l *LinkedList[T]) Pop() (element T, err error)
- func (l *LinkedList[T]) PopLeft() (element T, err error)
- func (l LinkedList[T]) Reduce(handler func(T, T) T, initial ...T) (result T, err error)
- func (l LinkedList[T]) ReduceRight(handler func(T, T) T, initial ...T) (result T, err error)
- func (l *LinkedList[T]) Remove(element T, counts ...int) *LinkedList[T]
- func (l *LinkedList[T]) RemoveByIndex(index int) (element T, err error)
- func (l *LinkedList[T]) RemoveIf(condition func(T) bool, counts ...int) LinkedList[T]
- func (l *LinkedList[T]) RemoveRight(element T, counts ...int) *LinkedList[T]
- func (l *LinkedList[T]) RemoveRightIf(condition func(T) bool, counts ...int) LinkedList[T]
- func (l *LinkedList[T]) Replace(oldElement, newElement T, counts ...int) *LinkedList[T]
- func (l *LinkedList[T]) ReplaceIf(condition func(T) bool, newElement T, counts ...int) LinkedList[T]
- func (l *LinkedList[T]) ReplaceRight(oldElement, newElement T, counts ...int) *LinkedList[T]
- func (l *LinkedList[T]) ReplaceRightIf(condition func(T) bool, newElement T, counts ...int) LinkedList[T]
- func (l *LinkedList[T]) Reverse() *LinkedList[T]
- func (l *LinkedList[T]) Rotate(steps ...int) *LinkedList[T]
- func (l *LinkedList[T]) Set(index int, element T) (err error)
- func (l LinkedList[T]) Slice(args ...int) LinkedList[T]
- func (l LinkedList[T]) Some(condition func(T) bool) bool
- func (l *LinkedList[T]) Splice(start, deleteCount int, items ...T) LinkedList[T]
- func (l LinkedList[T]) String() string
- func (l LinkedList[T]) Tail() (element T, err error)
- func (l LinkedList[T]) ToArray() []T
- func (l LinkedList[T]) ToReversed() LinkedList[T]
- func (l LinkedList[T]) ToSpliced(start, deleteCount int, items ...T) LinkedList[T]
- func (l *LinkedList[T]) UnmarshalJSON(data []byte) (err error)
- func (l LinkedList[T]) With(index int, value T) LinkedList[T]
Examples ¶
- LinkedList.Append
- LinkedList.AppendLeft
- LinkedList.At
- LinkedList.Clear
- LinkedList.Concat
- LinkedList.Copy
- LinkedList.Count
- LinkedList.Empty
- LinkedList.Equal
- LinkedList.Every
- LinkedList.Extend
- LinkedList.ExtendLeft
- LinkedList.Fill
- LinkedList.Filter
- LinkedList.Find
- LinkedList.FindIndex
- LinkedList.FindIndexes
- LinkedList.FindLast
- LinkedList.FindLastIndex
- LinkedList.FindLastIndexes
- LinkedList.FindLasts
- LinkedList.Finds
- LinkedList.ForEach
- LinkedList.Head
- LinkedList.Includes
- LinkedList.IndexOf
- LinkedList.Insert
- LinkedList.LastIndexOf
- LinkedList.Len
- LinkedList.Map
- LinkedList.MarshalJSON
- LinkedList.Pop
- LinkedList.PopLeft
- LinkedList.Reduce
- LinkedList.ReduceRight
- LinkedList.Remove
- LinkedList.RemoveByIndex
- LinkedList.RemoveIf
- LinkedList.RemoveRight
- LinkedList.RemoveRightIf
- LinkedList.Replace
- LinkedList.ReplaceIf
- LinkedList.ReplaceRight
- LinkedList.ReplaceRightIf
- LinkedList.Reverse
- LinkedList.Rotate
- LinkedList.Set
- LinkedList.Slice
- LinkedList.Some
- LinkedList.Splice
- LinkedList.String
- LinkedList.Tail
- LinkedList.ToReversed
- LinkedList.ToSpliced
- LinkedList.UnmarshalJSON
- LinkedList.With
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LinkedList ¶
type LinkedList[T any] struct { // contains filtered or unexported fields }
LinkedList represents a doubly linked list with references to the head, tail, and the size of the list.
func NewLinkedList ¶
func NewLinkedList[T any](elements ...T) *LinkedList[T]
NewLinkedList creates a new LinkedList with the given elements.
func (*LinkedList[T]) Append ¶
func (l *LinkedList[T]) Append(element T) *LinkedList[T]
Append appends a new element to the end of the list. Returns the modified list.
Example ¶
list := NewLinkedList(1, 2, 3) fmt.Println(list) list.Append(4) fmt.Println(list)
Output: [1 2 3] [1 2 3 4]
func (*LinkedList[T]) AppendLeft ¶
func (l *LinkedList[T]) AppendLeft(element T) *LinkedList[T]
AppendLeft appends a new element to the beginning of the list. Returns the modified list.
Example ¶
list := NewLinkedList(1, 2, 3) fmt.Println(list) list.AppendLeft(4) fmt.Println(list)
Output: [1 2 3] [4 1 2 3]
func (LinkedList[T]) At ¶
func (l LinkedList[T]) At(index int) (value T, err error)
At returns the element at the specified position in this list. If the index is out of range, the function returns an error.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) element, _ := list.At(2) fmt.Println(element) element, _ = list.At(-1) fmt.Println(element) _, err := list.At(10) fmt.Println(err)
Output: 3 5 the index is out of range
func (*LinkedList[T]) Clear ¶
func (l *LinkedList[T]) Clear() *LinkedList[T]
Clear removes all elements from the list.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(list) list.Clear() fmt.Println(list)
Output: [1 2 3 4 5] []
func (LinkedList[T]) Concat ¶
func (l LinkedList[T]) Concat(another LinkedList[T]) LinkedList[T]
Concat concatenates the current LinkedList with another LinkedList.
Example ¶
l1 := NewLinkedList(1, 2, 3) l2 := NewLinkedList(4, 5, 6) concatenated := l1.Concat(*l2) fmt.Println(concatenated)
Output: [1 2 3 4 5 6]
func (LinkedList[T]) Copy ¶
func (l LinkedList[T]) Copy() LinkedList[T]
Copy creates a copy of the LinkedList.
Example ¶
l := NewLinkedList(1, 2, 3, 4, 5) backup := l.Copy() fmt.Println(backup) fmt.Println(l.Equal(backup))
Output: [1 2 3 4 5] true
func (LinkedList[T]) Count ¶
func (l LinkedList[T]) Count(element T) (count int)
Count returns the number of occurrences of the given element in the linked list
Example ¶
ll := NewLinkedList(1, 2, 3, 4, 5, 5, 5) fmt.Println(ll.Count(5)) fmt.Println(ll.Count(6))
Output: 3 0
func (LinkedList[T]) Empty ¶
func (l LinkedList[T]) Empty() bool
Empty checks if the linked list is empty
Example ¶
ll := NewLinkedList[int]() fmt.Println(ll.Empty()) ll2 := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(ll2.Empty())
Output: true false
func (LinkedList[T]) Equal ¶
func (l LinkedList[T]) Equal(another LinkedList[T]) bool
Equal checks if this linked list is equal to another linked list
Example ¶
ll1 := NewLinkedList(1, 2, 3) ll2 := NewLinkedList(1, 2, 3) ll3 := NewLinkedList(1, 2, 3, 4) ll4 := NewLinkedList(1, 2, 4) fmt.Println(ll1.Equal(*ll2)) fmt.Println(ll1.Equal(*ll3)) fmt.Println(ll1.Equal(*ll4))
Output: true false false
func (LinkedList[T]) Every ¶
func (l LinkedList[T]) Every(condition func(T) bool) bool
Every checks if every element in the linked list satisfies the specified condition function.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) allEven := list.Every(func(item int) bool { return item%2 == 0 }) fmt.Println(allEven) allPositive := list.Every(func(item int) bool { return item > 0 }) fmt.Println(allPositive)
Output: false true
func (*LinkedList[T]) Extend ¶
func (l *LinkedList[T]) Extend(another *LinkedList[T]) *LinkedList[T]
Extend extends the list by appending all the elements from the specified list. Returns the modified list.
Example ¶
l1 := NewLinkedList(1, 2, 3) fmt.Println(l1) l2 := NewLinkedList(4, 5, 6) l1.Extend(l2) fmt.Println(l1)
Output: [1 2 3] [1 2 3 4 5 6]
func (*LinkedList[T]) ExtendLeft ¶
func (l *LinkedList[T]) ExtendLeft(another *LinkedList[T]) *LinkedList[T]
ExtendLeft extends the list by prepending all the elements from the specified list. Returns the modified list.
Example ¶
l1 := NewLinkedList(1, 2, 3) fmt.Println(l1) l2 := NewLinkedList(4, 5, 6) l1.ExtendLeft(l2) fmt.Println(l1)
Output: [1 2 3] [6 5 4 1 2 3]
func (*LinkedList[T]) Fill ¶
func (l *LinkedList[T]) Fill(element T, area ...int) *LinkedList[T]
Fill fills the specified area of the list with the specified element. If the area argument is not specified, the entire list will be filled with the specified element. Returns the modified list.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(list) list.Fill(0, 1, 3) fmt.Println(list)
Output: [1 2 3 4 5] [1 0 0 4 5]
func (LinkedList[T]) Filter ¶
func (l LinkedList[T]) Filter(condition func(T) bool) LinkedList[T]
Filter returns a new linked list containing elements for which the specified condition function returns true.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) condition := func(item int) bool { return item%2 == 0 } filteredList := list.Filter(condition) fmt.Println(filteredList)
Output: [2 4]
func (LinkedList[T]) Find ¶
func (l LinkedList[T]) Find(by func(T) bool) (element T, found bool)
Find returns the first element in this list that satisfies the given predicate, or nil if no such element is found.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) even := func(num int) bool { return num%2 == 0 } fmt.Println(list.Find(even)) negative := func(num int) bool { return num < 0 } fmt.Println(list.Find(negative))
Output: 2 true 0 false
func (LinkedList[T]) FindIndex ¶
func (l LinkedList[T]) FindIndex(by func(T) bool) (index int)
FindIndex returns the index of the first element in this list that satisfies the given predicate, or -1 if no such element is found.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) even := func(num int) bool { return num%2 == 0 } fmt.Println(list.FindIndex(even)) negative := func(num int) bool { return num < 0 } fmt.Println(list.FindIndex(negative))
Output: 1 -1
func (LinkedList[T]) FindIndexes ¶
func (l LinkedList[T]) FindIndexes(by func(T) bool, counts ...int) (indexes []int)
FindIndexes returns the indexes of elements in this list that satisfy the given predicate. If no count is specified, all elements that satisfy the predicate are returned.
Example ¶
l := NewLinkedList(1, 2, 3, 4, 5, 2) condition := func(val int) bool { return val > 2 } fmt.Println(l.FindIndexes(condition)) fmt.Println(l.FindIndexes(condition, 2))
Output: [2 3 4] [2 3]
func (LinkedList[T]) FindLast ¶
func (l LinkedList[T]) FindLast(by func(T) bool) (element T, found bool)
FindLast returns the last element in this list that satisfies the given predicate, or nil if no such element is found.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) even := func(num int) bool { return num%2 == 0 } fmt.Println(list.Find(even)) fmt.Println(list.FindLast(even))
Output: 2 true 4 true
func (LinkedList[T]) FindLastIndex ¶
func (l LinkedList[T]) FindLastIndex(by func(T) bool) (index int)
FindLastIndex returns the index of the last element in this list that satisfies the given predicate, or -1 if no such element is found.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) even := func(num int) bool { return num%2 == 0 } fmt.Println(list.FindIndex(even)) fmt.Println(list.FindLastIndex(even))
Output: 1 3
func (LinkedList[T]) FindLastIndexes ¶
func (l LinkedList[T]) FindLastIndexes(by func(T) bool, counts ...int) (indexes []int)
FindLastIndexes returns the indexes of elements in this list that satisfy the given predicate, starting from the end of the list. If no count is specified, all elements that satisfy the predicate are returned.
Example ¶
l := NewLinkedList(1, 2, 3, 4, 5, 2) condition := func(val int) bool { return val > 2 } fmt.Println(l.FindIndexes(condition)) fmt.Println(l.FindLastIndexes(condition))
Output: [2 3 4] [4 3 2]
func (LinkedList[T]) FindLasts ¶
func (l LinkedList[T]) FindLasts(by func(T) bool, counts ...int) (elements []T)
FindLasts returns the elements in this list that satisfy the given predicate, starting from the end of the list. If no count is specified, all elements that satisfy the predicate are returned.
Example ¶
l := NewLinkedList(1, 2, 3, 4, 5, 2) condition := func(val int) bool { return val > 2 } fmt.Println(l.Finds(condition)) fmt.Println(l.FindLasts(condition))
Output: [3 4 5] [5 4 3]
func (LinkedList[T]) Finds ¶
func (l LinkedList[T]) Finds(by func(T) bool, counts ...int) (elements []T)
Finds returns the elements in this list that satisfy the given predicate. If no count is specified, all elements that satisfy the predicate are returned.
Example ¶
l := NewLinkedList(1, 2, 3, 4, 5, 2) condition := func(val int) bool { return val > 2 } fmt.Println(l.Finds(condition)) fmt.Println(l.Finds(condition, 2))
Output: [3 4 5] [3 4]
func (*LinkedList[T]) ForEach ¶
func (l *LinkedList[T]) ForEach(action func(T) T) *LinkedList[T]
ForEach applies the specified action function to each element of the list. Returns the modified list.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(list) list.ForEach(func(item int) int { return item * 2 }) fmt.Println(list)
Output: [1 2 3 4 5] [2 4 6 8 10]
func (LinkedList[T]) Head ¶
func (l LinkedList[T]) Head() (element T, err error)
Head returns the first element in this list, or an error if the list is empty.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(list.Head()) fmt.Println(NewLinkedList[int]().Head())
Output: 1 <nil> 0 the input list is empty
func (LinkedList[T]) Includes ¶
func (l LinkedList[T]) Includes(element T) bool
Includes checks if the linked list includes the given element
Example ¶
ll := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(ll.Includes(3)) fmt.Println(ll.Includes(6))
Output: true false
func (LinkedList[T]) IndexOf ¶
func (l LinkedList[T]) IndexOf(element T) (index int)
IndexOf returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(list.IndexOf(3)) fmt.Println(list.IndexOf(6))
Output: 2 -1
func (*LinkedList[T]) Insert ¶
func (l *LinkedList[T]) Insert(index int, element T) *LinkedList[T]
Insert inserts a new element at the specified index. Returns the modified list.
Example ¶
list := NewLinkedList(1, 2, 3) fmt.Println(list) list.Insert(1, 5) fmt.Println(list)
Output: [1 2 3] [1 5 2 3]
func (LinkedList[T]) LastIndexOf ¶
func (l LinkedList[T]) LastIndexOf(element T) (index int)
LastIndexOf returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 3) fmt.Println(list.IndexOf(3)) fmt.Println(list.LastIndexOf(3))
Output: 2 4
func (LinkedList[T]) Len ¶
func (l LinkedList[T]) Len() int
Len returns the size of the linked list
Example ¶
ll := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(ll.Len())
Output: 5
func (LinkedList[T]) Map ¶
func (l LinkedList[T]) Map(handler func(T) T) LinkedList[T]
Map applies a function to each element of the linked list and returns a new linked list containing the results.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) handler := func(val int) int { return val * 2 } newList := list.Map(handler) fmt.Println(newList) fmt.Println(list)
Output: [2 4 6 8 10] [1 2 3 4 5]
func (LinkedList[T]) MarshalJSON ¶
func (l LinkedList[T]) MarshalJSON() ([]byte, error)
MarshalJSON converts the linked list to JSON
Example ¶
l := NewLinkedList(1, 2, 3) jsonData, _ := json.Marshal(l) fmt.Println(string(jsonData))
Output: [1,2,3]
func (*LinkedList[T]) Pop ¶
func (l *LinkedList[T]) Pop() (element T, err error)
Pop removes and returns the last element of the list.
Example ¶
l := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(l) element, _ := l.Pop() fmt.Println(element) fmt.Println(l)
Output: [1 2 3 4 5] 5 [1 2 3 4]
func (*LinkedList[T]) PopLeft ¶
func (l *LinkedList[T]) PopLeft() (element T, err error)
PopLeft removes and returns the first element of the list.
Example ¶
l := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(l) element, _ := l.PopLeft() fmt.Println(element) fmt.Println(l)
Output: [1 2 3 4 5] 1 [2 3 4 5]
func (LinkedList[T]) Reduce ¶
func (l LinkedList[T]) Reduce(handler func(T, T) T, initial ...T) (result T, err error)
Reduce reduces the linked list to a single value by applying a function cumulatively to the elements and an initial value (if provided).
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) // Example 1: Summing up the elements of the list sum, _ := list.Reduce(func(a, b int) int { return a + b }) fmt.Println(sum) // Example 2: Finding the maximum element in the list max, _ := list.Reduce(func(a, b int) int { if a > b { return a } return b }, 0) fmt.Println(max) // Example 3: Summing up the elements of the list with initial value sum, _ = list.Reduce(func(a, b int) int { return a + b }, 10) fmt.Println(sum)
Output: 15 5 25
func (LinkedList[T]) ReduceRight ¶
func (l LinkedList[T]) ReduceRight(handler func(T, T) T, initial ...T) (result T, err error)
ReduceRight reduces the linked list to a single value by applying a function cumulatively to the elements from right to left and an initial value (if provided).
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) f := func(a, b int) int { return a - b } result1, _ := list.Reduce(f) fmt.Println(result1) result2, _ := list.ReduceRight(f) fmt.Println(result2)
Output: -13 -5
func (*LinkedList[T]) Remove ¶
func (l *LinkedList[T]) Remove(element T, counts ...int) *LinkedList[T]
Remove removes the occurrences of the specified element from the list. If the count argument is not specified, only the first occurrence of the element will be removed. Returns the modified list.
Example ¶
l := NewLinkedList(1, 2, 2, 3, 4, 4, 4, 5, 5, 5, 5, 5) fmt.Println(l) l.Remove(2) fmt.Println(l) l.Remove(4, -1) fmt.Println(l) l.Remove(5, 2) fmt.Println(l)
Output: [1 2 2 3 4 4 4 5 5 5 5 5] [1 2 3 4 4 4 5 5 5 5 5] [1 2 3 5 5 5 5 5] [1 2 3 5 5 5]
func (*LinkedList[T]) RemoveByIndex ¶
func (l *LinkedList[T]) RemoveByIndex(index int) (element T, err error)
RemoveByIndex removes the element at the specified index. Returns the removed element and an error if the index is out of range.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(list) value, _ := list.RemoveByIndex(2) fmt.Println(value) fmt.Println(list)
Output: [1 2 3 4 5] 3 [1 2 4 5]
func (*LinkedList[T]) RemoveIf ¶
func (l *LinkedList[T]) RemoveIf(condition func(T) bool, counts ...int) LinkedList[T]
RemoveIf removes the elements that satisfy the specified condition from the list. If the count argument is not specified, only the first element that satisfies the condition will be removed. Returns the removed elements as a new list.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) evenCondition := func(val int) bool { return val%2 == 0 } fmt.Println(list) removed := list.RemoveIf(evenCondition, -1) fmt.Println(list) fmt.Println(removed)
Output: [1 2 3 4 5 6 7 8 9 10] [1 3 5 7 9] [2 4 6 8 10]
func (*LinkedList[T]) RemoveRight ¶
func (l *LinkedList[T]) RemoveRight(element T, counts ...int) *LinkedList[T]
RemoveRight removes the occurrences of the specified element from the right end of the list. If the count argument is not specified, only the first occurrence of the element will be removed. Returns the modified list.
Example ¶
l := NewLinkedList(1, 2, 3, 4, 5, 4, 3, 2, 1) l2 := l.Copy() fmt.Println(l) l.Remove(4) l2.RemoveRight(4) fmt.Println(l) fmt.Println(l2)
Output: [1 2 3 4 5 4 3 2 1] [1 2 3 5 4 3 2 1] [1 2 3 4 5 3 2 1]
func (*LinkedList[T]) RemoveRightIf ¶
func (l *LinkedList[T]) RemoveRightIf(condition func(T) bool, counts ...int) LinkedList[T]
RemoveRightIf removes the elements that satisfy the specified condition from the right end of the list. If the count argument is not specified, only the first element that satisfies the condition will be removed. Returns the removed elements as a new list.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) list2 := list.Copy() condition := func(val int) bool { return val%2 == 0 } fmt.Println(list) removed1 := list.RemoveRightIf(condition, 3) removed2 := list2.RemoveIf(condition, 3) fmt.Println(list) fmt.Println(removed1) fmt.Println(list2) fmt.Println(removed2)
Output: [1 2 3 4 5 6 7 8 9 10] [1 2 3 4 5 7 9] [10 8 6] [1 3 5 7 8 9 10] [2 4 6]
func (*LinkedList[T]) Replace ¶
func (l *LinkedList[T]) Replace(oldElement, newElement T, counts ...int) *LinkedList[T]
Replace replaces occurences of the specified old element with the new element. If the count argument is not specified, only the first occurrence of the old element will be replaced. Returns the modified list.
Example ¶
l := NewLinkedList(1, 2, 2, 3, 4, 4, 4, 5, 5, 5, 5, 5) fmt.Println(l) l.Replace(2, -2) fmt.Println(l) l.Replace(4, -4, -1) fmt.Println(l) l.Replace(5, -5, 2) fmt.Println(l)
Output: [1 2 2 3 4 4 4 5 5 5 5 5] [1 -2 2 3 4 4 4 5 5 5 5 5] [1 -2 2 3 -4 -4 -4 5 5 5 5 5] [1 -2 2 3 -4 -4 -4 -5 -5 5 5 5]
func (*LinkedList[T]) ReplaceIf ¶
func (l *LinkedList[T]) ReplaceIf(condition func(T) bool, newElement T, counts ...int) LinkedList[T]
ReplaceIf replaces the elements that satisfy the specified condition with the specified new element. If the count argument is not specified, only the first element that satisfies the condition will be replaced. Returns the replaced elements as a new list.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) evenCondition := func(val int) bool { return val%2 == 0 } fmt.Println(list) replaced := list.ReplaceIf(evenCondition, -1, -1) fmt.Println(list) fmt.Println(replaced)
Output: [1 2 3 4 5 6 7 8 9 10] [1 -1 3 -1 5 -1 7 -1 9 -1] [2 4 6 8 10]
func (*LinkedList[T]) ReplaceRight ¶
func (l *LinkedList[T]) ReplaceRight(oldElement, newElement T, counts ...int) *LinkedList[T]
ReplaceRight replaces occurences of the specified old element with the new element from the right end of the list. If the count argument is not specified, only the first occurrence of the old element will be replaced. Returns the modified list.
Example ¶
l := NewLinkedList(1, 2, 3, 4, 5, 4, 3, 2, 1) l2 := l.Copy() fmt.Println(l) l.Replace(4, 0) l2.ReplaceRight(4, 0) fmt.Println(l) fmt.Println(l2)
Output: [1 2 3 4 5 4 3 2 1] [1 2 3 0 5 4 3 2 1] [1 2 3 4 5 0 3 2 1]
func (*LinkedList[T]) ReplaceRightIf ¶
func (l *LinkedList[T]) ReplaceRightIf(condition func(T) bool, newElement T, counts ...int) LinkedList[T]
ReplaceRightIf replaces the elements that satisfy the specified condition with the specified new element from the right end of the list. If the count argument is not specified, only the first element that satisfies the condition will be replaced. Returns the replaced elements as a new list.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) list2 := list.Copy() condition := func(val int) bool { return val%2 == 0 } fmt.Println(list) replaced1 := list.ReplaceRightIf(condition, 0, 3) replaced2 := list2.ReplaceIf(condition, 0, 3) fmt.Println(list) fmt.Println(replaced1) fmt.Println(list2) fmt.Println(replaced2)
Output: [1 2 3 4 5 6 7 8 9 10] [1 2 3 4 5 0 7 0 9 0] [10 8 6] [1 0 3 0 5 0 7 8 9 10] [2 4 6]
func (*LinkedList[T]) Reverse ¶
func (l *LinkedList[T]) Reverse() *LinkedList[T]
Reverse reverses the order of the elements in the list. Returns the modified list.
Example ¶
l := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(l) l.Reverse() fmt.Println(l)
Output: [1 2 3 4 5] [5 4 3 2 1]
func (*LinkedList[T]) Rotate ¶
func (l *LinkedList[T]) Rotate(steps ...int) *LinkedList[T]
Rotate rotates the list by the specified number of steps. If the count argument is not specified, only one step will be performed. If the count is negative, the list rotates in a reverse direction. Returns the modified list.
Example ¶
list := NewLinkedList(1, 2, 3) fmt.Println(list) // Rotate the linked list list.Rotate() fmt.Println(list) // Rotate the linked list by -2 steps list.Rotate(-2) fmt.Println(list)
Output: [1 2 3] [3 1 2] [2 3 1]
func (*LinkedList[T]) Set ¶
func (l *LinkedList[T]) Set(index int, element T) (err error)
Set sets the value of the element at the specified index to the specified element. Returns an error if the index is out of range.
Example ¶
list := NewLinkedList(1, 2, 3) fmt.Println(list) _ = list.Set(2, 6) fmt.Println(list) _ = list.Set(-2, 5) fmt.Println(list) err := list.Set(5, 6) fmt.Println(err) fmt.Println(list)
Output: [1 2 3] [1 2 6] [1 5 6] the index is out of range [1 5 6]
func (LinkedList[T]) Slice ¶
func (l LinkedList[T]) Slice(args ...int) LinkedList[T]
Slice returns a new LinkedList that is a subset of the original LinkedList based on the provided slice arguments.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) slice1 := list.Slice(1, 4) fmt.Println(slice1) slice2 := list.Slice(0, 3, 2) fmt.Println(slice2) slice3 := list.Slice(3, 0, -1) fmt.Println(slice3)
Output: [2 3 4] [1 3] [4 3 2]
func (LinkedList[T]) Some ¶
func (l LinkedList[T]) Some(condition func(T) bool) bool
Some checks if there's at least one element in the linked list satisfying the specified condition function.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) result := list.Some(func(i int) bool { return i > 3 }) fmt.Println(result) result = list.Some(func(i int) bool { return i > 5 }) fmt.Println(result)
Output: true false
func (*LinkedList[T]) Splice ¶
func (l *LinkedList[T]) Splice(start, deleteCount int, items ...T) LinkedList[T]
Splice removes the specified number of elements from the list starting at the specified index, and then inserts the specified elements at the same index. Returns the removed elements as a new list.
Example ¶
arr := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(arr) arr.Splice(2, 2, 6, 7, 8) fmt.Println(arr)
Output: [1 2 3 4 5] [1 2 6 7 8 5]
func (LinkedList[T]) String ¶
func (l LinkedList[T]) String() string
String returns a string representation of the linked list
Example ¶
l := NewLinkedList(1, 2, 3) fmt.Println(l)
Output: [1 2 3]
func (LinkedList[T]) Tail ¶
func (l LinkedList[T]) Tail() (element T, err error)
Tail returns the last element in this list, or an error if the list is empty.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) fmt.Println(list.Tail()) fmt.Println(NewLinkedList[int]().Tail())
Output: 5 <nil> 0 the input list is empty
func (LinkedList[T]) ToArray ¶
func (l LinkedList[T]) ToArray() []T
ToArray converts the linked list to an array
func (LinkedList[T]) ToReversed ¶
func (l LinkedList[T]) ToReversed() LinkedList[T]
ToReversed returns a new LinkedList with the elements in reverse order compared to the original LinkedList.
Example ¶
l := NewLinkedList(1, 2, 3, 4, 5) reversed := l.ToReversed() fmt.Println(reversed) fmt.Println(l)
Output: [5 4 3 2 1] [1 2 3 4 5]
func (LinkedList[T]) ToSpliced ¶
func (l LinkedList[T]) ToSpliced(start, deleteCount int, items ...T) LinkedList[T]
ToSpliced returns a new LinkedList with items spliced into the original LinkedList at the specified index.
Example ¶
arr := NewLinkedList(1, 2, 3, 4, 5) newArr := arr.ToSpliced(2, 2, 6, 7, 8) fmt.Println(newArr) fmt.Println(arr)
Output: [1 2 6 7 8 5] [1 2 3 4 5]
func (*LinkedList[T]) UnmarshalJSON ¶
func (l *LinkedList[T]) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON populates the linked list from JSON data
Example ¶
jsonData := []byte("[4,5,6]") var l LinkedList[int] _ = json.Unmarshal(jsonData, &l) fmt.Println(l)
Output: [4 5 6]
func (LinkedList[T]) With ¶
func (l LinkedList[T]) With(index int, value T) LinkedList[T]
With returns a new LinkedList with the value at the specified index replaced with the given value.
Example ¶
list := NewLinkedList(1, 2, 3, 4, 5) newList := list.With(2, 10) fmt.Println(newList) fmt.Println(list)
Output: [1 2 10 4 5] [1 2 3 4 5]