Skip to content

Commit

Permalink
Merge pull request #227 from thebenkogan/master
Browse files Browse the repository at this point in the history
fix: doublylinkedlist insertion with last to first traversal
  • Loading branch information
emirpasic authored Sep 4, 2023
2 parents 702a6b2 + 6087664 commit 10d6c5b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
9 changes: 5 additions & 4 deletions lists/doublylinkedlist/doublylinkedlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (list *List) Values() []interface{} {
return values
}

//IndexOf returns index of provided element
// IndexOf returns index of provided element
func (list *List) IndexOf(value interface{}) int {
if list.size == 0 {
return -1
Expand Down Expand Up @@ -252,15 +252,14 @@ func (list *List) Insert(index int, values ...interface{}) {
return
}

list.size += len(values)

var beforeElement *element
var foundElement *element
// determine traversal direction, last to first or first to last
if list.size-index < index {
foundElement = list.last
beforeElement = list.last.prev
for e := list.size - 1; e != index; e, foundElement = e-1, foundElement.prev {
beforeElement = foundElement.prev
beforeElement = beforeElement.prev
}
} else {
foundElement = list.first
Expand Down Expand Up @@ -294,6 +293,8 @@ func (list *List) Insert(index int, values ...interface{}) {
oldNextElement.prev = beforeElement
beforeElement.next = oldNextElement
}

list.size += len(values)
}

// Set value at specified index position
Expand Down
19 changes: 13 additions & 6 deletions lists/doublylinkedlist/doublylinkedlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,24 @@ func TestListIndexOf(t *testing.T) {

func TestListInsert(t *testing.T) {
list := New()
list.Insert(0, "b", "c")
list.Insert(0, "b", "c", "d")
list.Insert(0, "a")
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 {
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)
}
}
Expand Down

0 comments on commit 10d6c5b

Please sign in to comment.