From 9f6dbf940a7fd00e6b25fea7b95b0ea222efc8a9 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Sat, 14 Mar 2015 00:36:10 +0100 Subject: [PATCH] add swap method on all lists --- lists/arraylist/arraylist.go | 2 +- lists/doublylinkedlist/doublylinkedlist.go | 16 ++++++++++ .../doublylinkedlist/doublylinkedlist_test.go | 31 +++++++++---------- lists/lists.go | 1 + lists/singlylinkedlist/singlylinkedlist.go | 16 ++++++++++ .../singlylinkedlist/singlylinkedlist_test.go | 31 +++++++++---------- 6 files changed, 64 insertions(+), 33 deletions(-) diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index 6473332..f98b5e5 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -142,7 +142,7 @@ func (list *List) Sort(comparator utils.Comparator) { utils.Sort(list.elements[:list.size], comparator) } -// Swaps two elements with the given indices. +// Swaps values of two elements at the given indices. func (list *List) Swap(i, j int) { if list.withinRange(i) && list.withinRange(j) { list.elements[i], list.elements[j] = list.elements[j], list.elements[i] diff --git a/lists/doublylinkedlist/doublylinkedlist.go b/lists/doublylinkedlist/doublylinkedlist.go index ec1843a..ed40eeb 100644 --- a/lists/doublylinkedlist/doublylinkedlist.go +++ b/lists/doublylinkedlist/doublylinkedlist.go @@ -228,6 +228,22 @@ func (list *List) Sort(comparator utils.Comparator) { } +// Swaps values of two elements at the given indices. +func (list *List) Swap(i, j int) { + if list.withinRange(i) && list.withinRange(j) && i != j { + var element1, element2 *element + for e, currentElement := 0, list.first; element1 == nil || element2 == nil; e, currentElement = e+1, currentElement.next { + switch e { + case i: + element1 = currentElement + case j: + element2 = currentElement + } + } + element1.value, element2.value = element2.value, element1.value + } +} + func (list *List) String() string { str := "DoublyLinkedList\n" values := []string{} diff --git a/lists/doublylinkedlist/doublylinkedlist_test.go b/lists/doublylinkedlist/doublylinkedlist_test.go index 910a32f..0c78c7b 100644 --- a/lists/doublylinkedlist/doublylinkedlist_test.go +++ b/lists/doublylinkedlist/doublylinkedlist_test.go @@ -37,16 +37,7 @@ func TestDoublyLinkedList(t *testing.T) { list.Sort(utils.StringComparator) - list.Add("g", "a") - list.Append("b", "c", "d") - list.Prepend("e", "f") - - shouldBe := []interface{}{"e", "f", "g", "a", "b", "c", "d"} - for i, _ := range shouldBe { - if value, ok := list.Get(i); value != shouldBe[i] || !ok { - t.Errorf("List not populated in correct order. Expected: %v Got: %v", shouldBe, list.Values()) - } - } + list.Add("e", "f", "g", "a", "b", "c", "d") list.Sort(utils.StringComparator) for i := 1; i < list.Size(); i++ { @@ -82,6 +73,14 @@ func TestDoublyLinkedList(t *testing.T) { t.Errorf("Got %v expected %v", actualValue, "c") } + list.Swap(0, 2) + list.Swap(0, 2) + list.Swap(0, 1) + + if actualValue, ok := list.Get(0); actualValue != "b" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } + list.Remove(2) if actualValue, ok := list.Get(2); actualValue != nil || ok { @@ -101,9 +100,9 @@ func TestDoublyLinkedList(t *testing.T) { list.Add("a", "b", "c") - //if actualValue := list.Contains("a", "b", "c"); actualValue != true { - // t.Errorf("Got %v expected %v", actualValue, true) - //} + if actualValue := list.Contains("a", "b", "c"); actualValue != true { + t.Errorf("Got %v expected %v", actualValue, true) + } if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) @@ -111,9 +110,9 @@ func TestDoublyLinkedList(t *testing.T) { list.Clear() - //if actualValue := list.Contains("a"); actualValue != false { - // t.Errorf("Got %v expected %v", actualValue, false) - //} + if actualValue := list.Contains("a"); actualValue != false { + t.Errorf("Got %v expected %v", actualValue, false) + } if actualValue, ok := list.Get(0); actualValue != nil || ok { t.Errorf("Got %v expected %v", actualValue, false) diff --git a/lists/lists.go b/lists/lists.go index 2a01c05..7e03285 100644 --- a/lists/lists.go +++ b/lists/lists.go @@ -29,6 +29,7 @@ type Interface interface { Add(elements ...interface{}) Contains(elements ...interface{}) bool Sort(comparator utils.Comparator) + Swap(index1, index2 int) containers.Interface // Empty() bool diff --git a/lists/singlylinkedlist/singlylinkedlist.go b/lists/singlylinkedlist/singlylinkedlist.go index 30d53da..a9b4d63 100644 --- a/lists/singlylinkedlist/singlylinkedlist.go +++ b/lists/singlylinkedlist/singlylinkedlist.go @@ -207,6 +207,22 @@ func (list *List) Sort(comparator utils.Comparator) { } +// Swaps values of two elements at the given indices. +func (list *List) Swap(i, j int) { + if list.withinRange(i) && list.withinRange(j) && i != j { + var element1, element2 *element + for e, currentElement := 0, list.first; element1 == nil || element2 == nil; e, currentElement = e+1, currentElement.next { + switch e { + case i: + element1 = currentElement + case j: + element2 = currentElement + } + } + element1.value, element2.value = element2.value, element1.value + } +} + func (list *List) String() string { str := "SinglyLinkedList\n" values := []string{} diff --git a/lists/singlylinkedlist/singlylinkedlist_test.go b/lists/singlylinkedlist/singlylinkedlist_test.go index 50c0825..4956507 100644 --- a/lists/singlylinkedlist/singlylinkedlist_test.go +++ b/lists/singlylinkedlist/singlylinkedlist_test.go @@ -37,16 +37,7 @@ func TestSinglyLinkedList(t *testing.T) { list.Sort(utils.StringComparator) - list.Add("g", "a") - list.Append("b", "c", "d") - list.Prepend("e", "f") - - shouldBe := []interface{}{"e", "f", "g", "a", "b", "c", "d"} - for i, _ := range shouldBe { - if value, ok := list.Get(i); value != shouldBe[i] || !ok { - t.Errorf("List not populated in correct order. Expected: %v Got: %v", shouldBe, list.Values()) - } - } + list.Add("e", "f", "g", "a", "b", "c", "d") list.Sort(utils.StringComparator) for i := 1; i < list.Size(); i++ { @@ -82,6 +73,14 @@ func TestSinglyLinkedList(t *testing.T) { t.Errorf("Got %v expected %v", actualValue, "c") } + list.Swap(0, 2) + list.Swap(0, 2) + list.Swap(0, 1) + + if actualValue, ok := list.Get(0); actualValue != "b" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } + list.Remove(2) if actualValue, ok := list.Get(2); actualValue != nil || ok { @@ -101,9 +100,9 @@ func TestSinglyLinkedList(t *testing.T) { list.Add("a", "b", "c") - //if actualValue := list.Contains("a", "b", "c"); actualValue != true { - // t.Errorf("Got %v expected %v", actualValue, true) - //} + if actualValue := list.Contains("a", "b", "c"); actualValue != true { + t.Errorf("Got %v expected %v", actualValue, true) + } if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) @@ -111,9 +110,9 @@ func TestSinglyLinkedList(t *testing.T) { list.Clear() - //if actualValue := list.Contains("a"); actualValue != false { - // t.Errorf("Got %v expected %v", actualValue, false) - //} + if actualValue := list.Contains("a"); actualValue != false { + t.Errorf("Got %v expected %v", actualValue, false) + } if actualValue, ok := list.Get(0); actualValue != nil || ok { t.Errorf("Got %v expected %v", actualValue, false)