add swap method on all lists

pull/1/head
Emir Pasic 10 years ago
parent c50f07c2ac
commit 9f6dbf940a

@ -142,7 +142,7 @@ func (list *List) Sort(comparator utils.Comparator) {
utils.Sort(list.elements[:list.size], 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) { func (list *List) Swap(i, j int) {
if list.withinRange(i) && list.withinRange(j) { if list.withinRange(i) && list.withinRange(j) {
list.elements[i], list.elements[j] = list.elements[j], list.elements[i] list.elements[i], list.elements[j] = list.elements[j], list.elements[i]

@ -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 { func (list *List) String() string {
str := "DoublyLinkedList\n" str := "DoublyLinkedList\n"
values := []string{} values := []string{}

@ -37,16 +37,7 @@ func TestDoublyLinkedList(t *testing.T) {
list.Sort(utils.StringComparator) list.Sort(utils.StringComparator)
list.Add("g", "a") list.Add("e", "f", "g", "a", "b", "c", "d")
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.Sort(utils.StringComparator) list.Sort(utils.StringComparator)
for i := 1; i < list.Size(); i++ { for i := 1; i < list.Size(); i++ {
@ -82,6 +73,14 @@ func TestDoublyLinkedList(t *testing.T) {
t.Errorf("Got %v expected %v", actualValue, "c") 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) list.Remove(2)
if actualValue, ok := list.Get(2); actualValue != nil || ok { if actualValue, ok := list.Get(2); actualValue != nil || ok {
@ -101,9 +100,9 @@ func TestDoublyLinkedList(t *testing.T) {
list.Add("a", "b", "c") list.Add("a", "b", "c")
//if actualValue := list.Contains("a", "b", "c"); actualValue != true { if actualValue := list.Contains("a", "b", "c"); actualValue != true {
// t.Errorf("Got %v expected %v", actualValue, true) t.Errorf("Got %v expected %v", actualValue, true)
//} }
if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false { if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false) t.Errorf("Got %v expected %v", actualValue, false)
@ -111,9 +110,9 @@ func TestDoublyLinkedList(t *testing.T) {
list.Clear() list.Clear()
//if actualValue := list.Contains("a"); actualValue != false { if actualValue := list.Contains("a"); actualValue != false {
// t.Errorf("Got %v expected %v", actualValue, false) t.Errorf("Got %v expected %v", actualValue, false)
//} }
if actualValue, ok := list.Get(0); actualValue != nil || ok { if actualValue, ok := list.Get(0); actualValue != nil || ok {
t.Errorf("Got %v expected %v", actualValue, false) t.Errorf("Got %v expected %v", actualValue, false)

@ -29,6 +29,7 @@ type Interface interface {
Add(elements ...interface{}) Add(elements ...interface{})
Contains(elements ...interface{}) bool Contains(elements ...interface{}) bool
Sort(comparator utils.Comparator) Sort(comparator utils.Comparator)
Swap(index1, index2 int)
containers.Interface containers.Interface
// Empty() bool // Empty() bool

@ -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 { func (list *List) String() string {
str := "SinglyLinkedList\n" str := "SinglyLinkedList\n"
values := []string{} values := []string{}

@ -37,16 +37,7 @@ func TestSinglyLinkedList(t *testing.T) {
list.Sort(utils.StringComparator) list.Sort(utils.StringComparator)
list.Add("g", "a") list.Add("e", "f", "g", "a", "b", "c", "d")
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.Sort(utils.StringComparator) list.Sort(utils.StringComparator)
for i := 1; i < list.Size(); i++ { for i := 1; i < list.Size(); i++ {
@ -82,6 +73,14 @@ func TestSinglyLinkedList(t *testing.T) {
t.Errorf("Got %v expected %v", actualValue, "c") 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) list.Remove(2)
if actualValue, ok := list.Get(2); actualValue != nil || ok { if actualValue, ok := list.Get(2); actualValue != nil || ok {
@ -101,9 +100,9 @@ func TestSinglyLinkedList(t *testing.T) {
list.Add("a", "b", "c") list.Add("a", "b", "c")
//if actualValue := list.Contains("a", "b", "c"); actualValue != true { if actualValue := list.Contains("a", "b", "c"); actualValue != true {
// t.Errorf("Got %v expected %v", actualValue, true) t.Errorf("Got %v expected %v", actualValue, true)
//} }
if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false { if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false {
t.Errorf("Got %v expected %v", actualValue, false) t.Errorf("Got %v expected %v", actualValue, false)
@ -111,9 +110,9 @@ func TestSinglyLinkedList(t *testing.T) {
list.Clear() list.Clear()
//if actualValue := list.Contains("a"); actualValue != false { if actualValue := list.Contains("a"); actualValue != false {
// t.Errorf("Got %v expected %v", actualValue, false) t.Errorf("Got %v expected %v", actualValue, false)
//} }
if actualValue, ok := list.Get(0); actualValue != nil || ok { if actualValue, ok := list.Get(0); actualValue != nil || ok {
t.Errorf("Got %v expected %v", actualValue, false) t.Errorf("Got %v expected %v", actualValue, false)

Loading…
Cancel
Save