From f246a54621c93d64a821cd2d505546212ec73e67 Mon Sep 17 00:00:00 2001 From: Mahadev Date: Tue, 26 Dec 2017 19:18:50 +0530 Subject: [PATCH 1/3] Add IndexOf method to ArrayList --- lists/arraylist/arraylist.go | 12 ++++++++++++ lists/arraylist/arraylist_test.go | 30 +++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index f9e3543..e2fbcec 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -98,6 +98,18 @@ func (list *List) Values() []interface{} { return newElements } +//IndexOf returns index of provided element +func (list *List) IndexOf(value interface{}) int{ + if list.size == 0 { + return -1 + } + for index, element := range list.elements { + if element == value { + return index + } + } + return -1 +} // Empty returns true if list does not contain any elements. func (list *List) Empty() bool { return list.size == 0 diff --git a/lists/arraylist/arraylist_test.go b/lists/arraylist/arraylist_test.go index 58fd8b1..abbb2aa 100644 --- a/lists/arraylist/arraylist_test.go +++ b/lists/arraylist/arraylist_test.go @@ -6,8 +6,9 @@ package arraylist import ( "fmt" - "github.com/emirpasic/gods/utils" "testing" + + "github.com/emirpasic/gods/utils" ) func TestListAdd(t *testing.T) { @@ -25,6 +26,33 @@ func TestListAdd(t *testing.T) { } } +func TestListIndexOf(t *testing.T) { + list := New() + + expectedIndex := -1 + if index := list.IndexOf("a"); index != expectedIndex{ + t.Errorf("Got %v expected %v",index,expectedIndex) + } + + list.Add("a") + list.Add("b", "c") + + expectedIndex = 0 + if index := list.IndexOf("a"); index != expectedIndex{ + t.Errorf("Got %v expected %v",index,expectedIndex) + } + + expectedIndex = 1 + if index := list.IndexOf("b"); index != expectedIndex{ + t.Errorf("Got %v expected %v",index,expectedIndex) + } + + expectedIndex = 2 + if index := list.IndexOf("c"); index != expectedIndex{ + t.Errorf("Got %v expected %v",index,expectedIndex) + } +} + func TestListRemove(t *testing.T) { list := New() list.Add("a") From e709a4b5ea88c8f3f566c8c8cea95a3e529d52a4 Mon Sep 17 00:00:00 2001 From: Mahadev Date: Tue, 26 Dec 2017 19:19:18 +0530 Subject: [PATCH 2/3] Add IndexOf method to SinglyLinkedList --- lists/singlylinkedlist/singlylinkedlist.go | 12 +++++++++ .../singlylinkedlist/singlylinkedlist_test.go | 27 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lists/singlylinkedlist/singlylinkedlist.go b/lists/singlylinkedlist/singlylinkedlist.go index 139925b..3182af3 100644 --- a/lists/singlylinkedlist/singlylinkedlist.go +++ b/lists/singlylinkedlist/singlylinkedlist.go @@ -154,6 +154,18 @@ func (list *List) Values() []interface{} { return values } +//IndexOf returns index of provided element +func (list *List) IndexOf(value interface{}) int{ + if list.size == 0 { + return -1 + } + for index, element := range list.Values() { + if element == value { + return index + } + } + return -1 +} // Empty returns true if list does not contain any elements. func (list *List) Empty() bool { return list.size == 0 diff --git a/lists/singlylinkedlist/singlylinkedlist_test.go b/lists/singlylinkedlist/singlylinkedlist_test.go index 27daacc..0bebbb3 100644 --- a/lists/singlylinkedlist/singlylinkedlist_test.go +++ b/lists/singlylinkedlist/singlylinkedlist_test.go @@ -133,6 +133,33 @@ func TestListValues(t *testing.T) { } } +func TestListIndexOf(t *testing.T) { + list := New() + + expectedIndex := -1 + if index := list.IndexOf("a"); index != expectedIndex{ + t.Errorf("Got %v expected %v",index,expectedIndex) + } + + list.Add("a") + list.Add("b", "c") + + expectedIndex = 0 + if index := list.IndexOf("a"); index != expectedIndex{ + t.Errorf("Got %v expected %v",index,expectedIndex) + } + + expectedIndex = 1 + if index := list.IndexOf("b"); index != expectedIndex{ + t.Errorf("Got %v expected %v",index,expectedIndex) + } + + expectedIndex = 2 + if index := list.IndexOf("c"); index != expectedIndex{ + t.Errorf("Got %v expected %v",index,expectedIndex) + } +} + func TestListInsert(t *testing.T) { list := New() list.Insert(0, "b", "c") From 8a171863a010a24600ffbb2d86bcc2fe95083671 Mon Sep 17 00:00:00 2001 From: Mahadev Date: Tue, 26 Dec 2017 19:19:36 +0530 Subject: [PATCH 3/3] Add IndexOf method to DoublyLinkedList --- lists/doublylinkedlist/doublylinkedlist.go | 12 +++++++++ .../doublylinkedlist/doublylinkedlist_test.go | 27 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lists/doublylinkedlist/doublylinkedlist.go b/lists/doublylinkedlist/doublylinkedlist.go index bd85b18..881e0f7 100644 --- a/lists/doublylinkedlist/doublylinkedlist.go +++ b/lists/doublylinkedlist/doublylinkedlist.go @@ -173,6 +173,18 @@ func (list *List) Values() []interface{} { return values } +//IndexOf returns index of provided element +func (list *List) IndexOf(value interface{}) int { + if list.size == 0 { + return -1 + } + for index, element := range list.Values() { + if element == value { + return index + } + } + return -1 +} // Empty returns true if list does not contain any elements. func (list *List) Empty() bool { return list.size == 0 diff --git a/lists/doublylinkedlist/doublylinkedlist_test.go b/lists/doublylinkedlist/doublylinkedlist_test.go index 5fea3d0..444e272 100644 --- a/lists/doublylinkedlist/doublylinkedlist_test.go +++ b/lists/doublylinkedlist/doublylinkedlist_test.go @@ -133,6 +133,33 @@ func TestListValues(t *testing.T) { } } +func TestListIndexOf(t *testing.T) { + list := New() + + expectedIndex := -1 + if index := list.IndexOf("a"); index != expectedIndex{ + t.Errorf("Got %v expected %v",index,expectedIndex) + } + + list.Add("a") + list.Add("b", "c") + + expectedIndex = 0 + if index := list.IndexOf("a"); index != expectedIndex{ + t.Errorf("Got %v expected %v",index,expectedIndex) + } + + expectedIndex = 1 + if index := list.IndexOf("b"); index != expectedIndex{ + t.Errorf("Got %v expected %v",index,expectedIndex) + } + + expectedIndex = 2 + if index := list.IndexOf("c"); index != expectedIndex{ + t.Errorf("Got %v expected %v",index,expectedIndex) + } +} + func TestListInsert(t *testing.T) { list := New() list.Insert(0, "b", "c")