linkedlist

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package linkedlist provides a doubly linked list implementation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LinkedList

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

LinkedList represents a doubly linked list with a head, tail, and size.

func NewLinkedList

func NewLinkedList(elements ...any) *LinkedList

NewLinkedList creates a new LinkedList with the given elements.

func (*LinkedList) Append

func (l *LinkedList) Append(element any) *LinkedList

Append appends the specified element to the end of the linked list and returns the modified linked 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) AppendLeft

func (l *LinkedList) AppendLeft(element any) *LinkedList

AppendLeft appends the specified element to the beginning of the linked list and returns the modified linked 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) At

func (l LinkedList) At(index int) (value any, err error)

At returns the element at the specified index in the linked list.

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) Clear

func (l *LinkedList) Clear() *LinkedList

Clear clears the linked list and returns the modified linked 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) Concat

func (l LinkedList) Concat(another LinkedList) LinkedList

Concat returns a new LinkedList containing all the elements of the original LinkedList followed by the elements of 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) Copy

func (l LinkedList) Copy() LinkedList

Copy returns a new LinkedList containing the same elements as the original 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) Count

func (l LinkedList) Count(element any) (count int)

Count returns the number of occurrences of a specific 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) Empty

func (l LinkedList) Empty() bool

Empty checks if the linked list is empty.

Example
ll := NewLinkedList()
fmt.Println(ll.Empty())
ll2 := NewLinkedList(1, 2, 3, 4, 5)
fmt.Println(ll2.Empty())
Output:

true
false

func (LinkedList) Equal

func (l LinkedList) Equal(another LinkedList) bool

Equal checks if two linked lists are equal by comparing their elements.

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) Every

func (l LinkedList) Every(condition func(any) bool) bool

Every checks if every element in the linked list satisfies the condition function.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
allEven := list.Every(func(item any) bool {
	return item.(int)%2 == 0
})
fmt.Println(allEven)
allPositive := list.Every(func(item any) bool {
	return item.(int) > 0
})
fmt.Println(allPositive)
Output:

false
true

func (*LinkedList) Extend

func (l *LinkedList) Extend(another *LinkedList) *LinkedList

Extend appends all elements from another linked list to the end of the current linked list and returns the modified linked 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) ExtendLeft

func (l *LinkedList) ExtendLeft(another *LinkedList) *LinkedList

ExtendLeft appends all elements from another linked list to the beginning of the current linked list and returns the modified linked 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) Fill

func (l *LinkedList) Fill(element any, area ...int) *LinkedList

Fill fills the specified area of the linked list with the given element and returns the modified linked 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) Filter

func (l LinkedList) Filter(condition func(any) bool) LinkedList

Filter returns a new linked list containing only the elements for which the condition function returns true.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
condition := func(item any) bool {
	return item.(int)%2 == 0
}
filteredList := list.Filter(condition)
fmt.Println(filteredList)
Output:

[2 4]

func (LinkedList) Find

func (l LinkedList) Find(by func(any) bool) (element any, found bool)

Find returns the first element in the linked list for which the given function returns true.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
even := func(num any) bool {
	return num.(int)%2 == 0
}
fmt.Println(list.Find(even))
negative := func(num any) bool {
	return num.(int) < 0
}
fmt.Println(list.Find(negative))
Output:

2 true
<nil> false

func (LinkedList) FindIndex

func (l LinkedList) FindIndex(by func(any) bool) (index int)

FindIndex returns the index of the first element in the linked list for which the given function returns true.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
even := func(num any) bool {
	return num.(int)%2 == 0
}
fmt.Println(list.FindIndex(even))
negative := func(num any) bool {
	return num.(int) < 0
}
fmt.Println(list.FindIndex(negative))
Output:

1
-1

func (LinkedList) FindIndexes

func (l LinkedList) FindIndexes(by func(any) bool, counts ...int) (indexes []int)

FindIndexes returns the indexes of the elements in the linked list for which the given function returns true.

Example
l := NewLinkedList(1, 2, 3, 4, 5, 2)
condition := func(val any) bool {
	return val.(int) > 2
}
fmt.Println(l.FindIndexes(condition))
fmt.Println(l.FindIndexes(condition, 2))
Output:

[2 3 4]
[2 3]

func (LinkedList) FindLast

func (l LinkedList) FindLast(by func(any) bool) (element any, found bool)

FindLast returns the last element in the linked list for which the given function returns true.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
even := func(num any) bool {
	return num.(int)%2 == 0
}
fmt.Println(list.Find(even))
fmt.Println(list.FindLast(even))
Output:

2 true
4 true

func (LinkedList) FindLastIndex

func (l LinkedList) FindLastIndex(by func(any) bool) (index int)

FindLastIndex returns the index of the last element in the linked list for which the given function returns true.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
even := func(num any) bool {
	return num.(int)%2 == 0
}
fmt.Println(list.FindIndex(even))
fmt.Println(list.FindLastIndex(even))
Output:

1
3

func (LinkedList) FindLastIndexes

func (l LinkedList) FindLastIndexes(by func(any) bool, counts ...int) (indexes []int)

FindLastIndexes returns the indexes of the last elements in the linked list for which the given function returns true.

Example
l := NewLinkedList(1, 2, 3, 4, 5, 2)
condition := func(val any) bool {
	return val.(int) > 2
}
fmt.Println(l.FindIndexes(condition))
fmt.Println(l.FindLastIndexes(condition))
Output:

[2 3 4]
[4 3 2]

func (LinkedList) FindLasts

func (l LinkedList) FindLasts(by func(any) bool, counts ...int) (elements []any)

FindLasts returns the last elements in the linked list for which the given function returns true.

Example
l := NewLinkedList(1, 2, 3, 4, 5, 2)
condition := func(val any) bool {
	return val.(int) > 2
}
fmt.Println(l.Finds(condition))
fmt.Println(l.FindLasts(condition))
Output:

[3 4 5]
[5 4 3]

func (LinkedList) Finds

func (l LinkedList) Finds(by func(any) bool, counts ...int) (elements []any)

Finds returns the elements in the linked list for which the given function returns true.

Example
l := NewLinkedList(1, 2, 3, 4, 5, 2)
condition := func(val any) bool {
	return val.(int) > 2
}
fmt.Println(l.Finds(condition))
fmt.Println(l.Finds(condition, 2))
Output:

[3 4 5]
[3 4]

func (*LinkedList) ForEach

func (l *LinkedList) ForEach(action func(any) any) *LinkedList

ForEach performs the specified action on each element in the linked list and returns the modified linked list.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
fmt.Println(list)
list.ForEach(func(item any) any {
	return item.(int) * 2
})
fmt.Println(list)
Output:

[1 2 3 4 5]
[2 4 6 8 10]

func (LinkedList) Head

func (l LinkedList) Head() (element any, err error)

Head returns the first element of the linked list.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
fmt.Println(list.Head())
fmt.Println(NewLinkedList().Head())
Output:

1 <nil>
<nil> the input list is empty

func (LinkedList) Includes

func (l LinkedList) Includes(element any) bool

Includes checks if a specified element is included in the linked list.

Example
ll := NewLinkedList(1, 2, 3, 4, 5)
fmt.Println(ll.Includes(3))
fmt.Println(ll.Includes(6))
Output:

true
false

func (LinkedList) IndexOf

func (l LinkedList) IndexOf(element any) (index int)

IndexOf returns the index of the first occurrence of the specified element in the linked list.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
fmt.Println(list.IndexOf(3))
fmt.Println(list.IndexOf(6))
Output:

2
-1

func (*LinkedList) Insert

func (l *LinkedList) Insert(index int, element any) *LinkedList

Insert inserts the specified element at the specified index in the linked list and returns the modified linked 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) LastIndexOf

func (l LinkedList) LastIndexOf(element any) (index int)

LastIndexOf returns the index of the last occurrence of the specified element in the linked list.

Example
list := NewLinkedList(1, 2, 3, 4, 3)
fmt.Println(list.IndexOf(3))
fmt.Println(list.LastIndexOf(3))
Output:

2
4

func (LinkedList) Len

func (l LinkedList) 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) Map

func (l LinkedList) Map(handler func(any) any) LinkedList

Map applies the handler function to each element in the linked list and returns a new linked list containing the results.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
handler := func(val any) any {
	return val.(int) * 2
}
newList := list.Map(handler)
fmt.Println(newList)
fmt.Println(list)
Output:

[2 4 6 8 10]
[1 2 3 4 5]

func (LinkedList) MarshalJSON

func (l LinkedList) MarshalJSON() ([]byte, error)

MarshalJSON converts the linked list to JSON format

Example
l := NewLinkedList(1, 2, 3)
jsonData, _ := json.Marshal(l)
fmt.Println(string(jsonData))
Output:

[1,2,3]

func (*LinkedList) Pop

func (l *LinkedList) Pop() (element any, err error)

Pop removes and returns the last element from the linked 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) PopLeft

func (l *LinkedList) PopLeft() (element any, err error)

PopLeft removes and returns the first element from the linked 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) Reduce

func (l LinkedList) Reduce(handler func(any, any) any, initial ...any) (result any, err error)

Reduce applies the handler function to each element in the linked list and returns a single result. The initial value, if provided, is used as the initial result; otherwise, the value of the first element is used.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
// Example 1: Summing up the elements of the list
sum, _ := list.Reduce(func(a, b any) any {
	return a.(int) + b.(int)
})
fmt.Println(sum)
// Example 2: Finding the maximum element in the list
max, _ := list.Reduce(func(a, b any) any {
	if a.(int) > b.(int) {
		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 any) any {
	return a.(int) + b.(int)
}, 10)
fmt.Println(sum)
Output:

15
5
25

func (LinkedList) ReduceRight

func (l LinkedList) ReduceRight(handler func(any, any) any, initial ...any) (result any, err error)

ReduceRight applies the handler function to each element in the linked list in reverse order and returns a single result. The initial value, if provided, is used as the initial result; otherwise, the value of the last element is used.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
f := func(a, b any) any {
	return a.(int) - b.(int)
}
result1, _ := list.Reduce(f)
fmt.Println(result1)
result2, _ := list.ReduceRight(f)
fmt.Println(result2)
Output:

-13
-5

func (*LinkedList) Remove

func (l *LinkedList) Remove(element any, counts ...int) *LinkedList

Remove removes the specified element from the linked list and returns the modified linked 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) RemoveByIndex

func (l *LinkedList) RemoveByIndex(index int) (element any, err error)

RemoveByIndex removes and returns the element at the specified index in the linked list.

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) RemoveIf

func (l *LinkedList) RemoveIf(condition func(any) bool, counts ...int) LinkedList

RemoveIf removes elements that satisfy the specified condition and returns a new linked list containing the removed elements.

Example
list := NewLinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
evenCondition := func(val any) bool {
	num := val.(int)
	return num%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) RemoveRight

func (l *LinkedList) RemoveRight(element any, counts ...int) *LinkedList

RemoveRight removes the specified element from the right end of the linked list and returns the modified linked 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) RemoveRightIf

func (l *LinkedList) RemoveRightIf(condition func(any) bool, counts ...int) LinkedList

RemoveRightIf removes elements from the right end that satisfy the specified condition and returns a new linked list containing the removed elements.

Example
list := NewLinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
list2 := list.Copy()
condition := func(val any) bool {
	return val.(int)%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) Replace

func (l *LinkedList) Replace(oldElement, newElement any, counts ...int) *LinkedList

Replace replaces all occurrences of the specified old element with the new element and returns the modified linked 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) ReplaceIf

func (l *LinkedList) ReplaceIf(condition func(any) bool, newElement any, counts ...int) LinkedList

ReplaceIf replaces elements that satisfy the specified condition with the new element and returns a new linked list containing the original elements.

Example
list := NewLinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
evenCondition := func(val any) bool {
	num := val.(int)
	return num%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) ReplaceRight

func (l *LinkedList) ReplaceRight(oldElement, newElement any, counts ...int) *LinkedList

ReplaceRight replaces all occurrences of the specified old element with the new element from the right end and returns the modified linked 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) ReplaceRightIf

func (l *LinkedList) ReplaceRightIf(condition func(any) bool, newElement any, counts ...int) LinkedList

ReplaceRightIf replaces elements from the right end that satisfy the specified condition with the new element and returns a new linked list containing the original elements.

Example
list := NewLinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
list2 := list.Copy()
condition := func(val any) bool {
	return val.(int)%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) Reverse

func (l *LinkedList) Reverse() *LinkedList

Reverse reverses the order of elements in the linked list and returns the modified linked 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) Rotate

func (l *LinkedList) Rotate(steps ...int) *LinkedList

Rotate rotates the linked list by the specified number of steps and returns the modified linked 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) Set

func (l *LinkedList) Set(index int, element any) (err error)

Set sets the element at the specified index in the linked list and returns an error if the index is invalid.

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) Slice

func (l LinkedList) Slice(args ...int) LinkedList

Slice returns a new LinkedList containing elements from start to end with an optional step.

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) Some

func (l LinkedList) Some(condition func(any) bool) bool

Some checks if at least one element in the linked list satisfies the condition function.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
result := list.Some(func(i any) bool {
	return i.(int) > 3
})
fmt.Println(result)
result = list.Some(func(i any) bool {
	return i.(int) > 5
})
fmt.Println(result)
Output:

true
false

func (*LinkedList) Splice

func (l *LinkedList) Splice(start, deleteCount int, items ...any) LinkedList

Splice removes and/or adds elements in the linked list and returns a new linked list containing the removed elements.

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) String

func (l LinkedList) String() string

String returns the string representation of the linked list

Example
l := NewLinkedList(1, 2, 3)
fmt.Println(l)
Output:

[1 2 3]

func (LinkedList) Tail

func (l LinkedList) Tail() (element any, err error)

Tail returns the last element of the linked list.

Example
list := NewLinkedList(1, 2, 3, 4, 5)
fmt.Println(list.Tail())
fmt.Println(NewLinkedList().Tail())
Output:

5 <nil>
<nil> the input list is empty

func (LinkedList) ToArray

func (l LinkedList) ToArray() []any

ToArray converts the linked list to an array of elements.

func (LinkedList) ToReversed

func (l LinkedList) ToReversed() LinkedList

ToReversed returns a new LinkedList with the elements in reverse order.

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) ToSpliced

func (l LinkedList) ToSpliced(start, deleteCount int, items ...any) LinkedList

ToSpliced returns a new LinkedList with the elements starting at the specified index deleted and replaced by the new items.

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) UnmarshalJSON

func (l *LinkedList) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON populates the linked list from JSON-formatted data

Example
jsonData := []byte("[4,5,6]")
var l LinkedList
_ = json.Unmarshal(jsonData, &l)
fmt.Println(l)
Output:

[4 5 6]

func (LinkedList) With

func (l LinkedList) With(index int, value any) LinkedList

With returns a new LinkedList with the element at the specified index replaced by the new 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]

Jump to

Keyboard shortcuts

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