|
|
@ -11,9 +11,10 @@ package doublylinkedlist
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/emirpasic/gods/lists"
|
|
|
|
"github.com/emirpasic/gods/lists"
|
|
|
|
"github.com/emirpasic/gods/utils"
|
|
|
|
"github.com/emirpasic/gods/utils"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func assertListImplementation() {
|
|
|
|
func assertListImplementation() {
|
|
|
@ -292,6 +293,37 @@ func (list *List) Insert(index int, values ...interface{}) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Sets value at specified index position
|
|
|
|
|
|
|
|
// Does not do anything if position is negative or bigger than list's size
|
|
|
|
|
|
|
|
// Note: position equal to list's size is valid, i.e. append.
|
|
|
|
|
|
|
|
func (list *List) Set(index int, value interface{}) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !list.withinRange(index) {
|
|
|
|
|
|
|
|
// Append
|
|
|
|
|
|
|
|
if index == list.size {
|
|
|
|
|
|
|
|
list.Add(value)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var foundElement *element
|
|
|
|
|
|
|
|
// determine traversal direction, last to first or first to last
|
|
|
|
|
|
|
|
if list.size-index < index {
|
|
|
|
|
|
|
|
foundElement = list.last
|
|
|
|
|
|
|
|
for e := list.size - 1; e != index; {
|
|
|
|
|
|
|
|
fmt.Println("Set last", index, value, foundElement, foundElement.prev)
|
|
|
|
|
|
|
|
e, foundElement = e-1, foundElement.prev
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
foundElement = list.first
|
|
|
|
|
|
|
|
for e := 0; e != index; {
|
|
|
|
|
|
|
|
e, foundElement = e+1, foundElement.next
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foundElement.value = value
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// String returns a string representation of container
|
|
|
|
// String returns a string representation of container
|
|
|
|
func (list *List) String() string {
|
|
|
|
func (list *List) String() string {
|
|
|
|
str := "DoublyLinkedList\n"
|
|
|
|
str := "DoublyLinkedList\n"
|
|
|
|