Merge pull request #227 from thebenkogan/master

fix: doublylinkedlist insertion with last to first traversal
pull/235/head
Emir Pasic 9 months ago committed by GitHub
commit 10d6c5b4f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -177,7 +177,7 @@ func (list *List) Values() []interface{} {
return values return values
} }
//IndexOf returns index of provided element // IndexOf returns index of provided element
func (list *List) IndexOf(value interface{}) int { func (list *List) IndexOf(value interface{}) int {
if list.size == 0 { if list.size == 0 {
return -1 return -1
@ -252,15 +252,14 @@ func (list *List) Insert(index int, values ...interface{}) {
return return
} }
list.size += len(values)
var beforeElement *element var beforeElement *element
var foundElement *element var foundElement *element
// determine traversal direction, last to first or first to last // determine traversal direction, last to first or first to last
if list.size-index < index { if list.size-index < index {
foundElement = list.last foundElement = list.last
beforeElement = list.last.prev
for e := list.size - 1; e != index; e, foundElement = e-1, foundElement.prev { for e := list.size - 1; e != index; e, foundElement = e-1, foundElement.prev {
beforeElement = foundElement.prev beforeElement = beforeElement.prev
} }
} else { } else {
foundElement = list.first foundElement = list.first
@ -294,6 +293,8 @@ func (list *List) Insert(index int, values ...interface{}) {
oldNextElement.prev = beforeElement oldNextElement.prev = beforeElement
beforeElement.next = oldNextElement beforeElement.next = oldNextElement
} }
list.size += len(values)
} }
// Set value at specified index position // Set value at specified index position

@ -213,17 +213,24 @@ func TestListIndexOf(t *testing.T) {
func TestListInsert(t *testing.T) { func TestListInsert(t *testing.T) {
list := New() list := New()
list.Insert(0, "b", "c") list.Insert(0, "b", "c", "d")
list.Insert(0, "a") list.Insert(0, "a")
list.Insert(10, "x") // ignore list.Insert(10, "x") // ignore
if actualValue := list.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
list.Insert(3, "d") // append
if actualValue := list.Size(); actualValue != 4 { if actualValue := list.Size(); actualValue != 4 {
t.Errorf("Got %v expected %v", actualValue, 4) t.Errorf("Got %v expected %v", actualValue, 4)
} }
if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s", list.Values()...), "abcd"; actualValue != expectedValue { list.Insert(4, "g") // append
if actualValue := list.Size(); actualValue != 5 {
t.Errorf("Got %v expected %v", actualValue, 5)
}
if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s%s", list.Values()...), "abcdg"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
list.Insert(4, "e", "f") // last to first traversal
if actualValue := list.Size(); actualValue != 7 {
t.Errorf("Got %v expected %v", actualValue, 7)
}
if actualValue, expectedValue := fmt.Sprintf("%s%s%s%s%s%s%s", list.Values()...), "abcdefg"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue) t.Errorf("Got %v expected %v", actualValue, expectedValue)
} }
} }

Loading…
Cancel
Save