Implemented swapping for the linked liste.
This commit is contained in:
parent
63bff23ca4
commit
49635e09a3
2 changed files with 19 additions and 0 deletions
|
@ -15,6 +15,8 @@ func main() {
|
|||
ll.Append("four")
|
||||
ll.Push("minus one")
|
||||
|
||||
ll.Swap(1, 3)
|
||||
|
||||
for p := range ll.Chan() {
|
||||
fmt.Println(p.K, p.V)
|
||||
}
|
||||
|
|
|
@ -75,6 +75,23 @@ func (ll *LinkedList[V]) Set(i int, v V) (bool) {
|
|||
return true
|
||||
}
|
||||
|
||||
func (ll *LinkedList[V]) Swap(i1, i2 int) {
|
||||
if i1 == i2 {
|
||||
return
|
||||
}
|
||||
|
||||
max := ll.ln - 1
|
||||
if i1 < 0 || i2 < 0 || i1 > max || i2 > max {
|
||||
panic("index out of range")
|
||||
}
|
||||
|
||||
el1, _ := ll.GetEl(i1)
|
||||
el2, _ := ll.GetEl(i2)
|
||||
|
||||
el1.value, el2.value =
|
||||
el2.value, el1.value
|
||||
}
|
||||
|
||||
// Deletes the element by its index.
|
||||
func (ll *LinkedList[V]) Delete(i int) (bool) {
|
||||
if ll.ln <= i {
|
||||
|
|
Loading…
Reference in a new issue