Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions structure/linkedlist/singlylinkedlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func (ll *Singly[T]) DelAtEnd() (T, bool) {
cur.Next = nil
ll.length--
return retval, true

}

// DelByPos deletes the node at the middle based on position in the list
Expand All @@ -91,12 +90,12 @@ func (ll *Singly[T]) DelByPos(pos int) (T, bool) {
case ll.Head == nil:
var r T
return r, false
case pos-1 > ll.length:
case pos > ll.length || pos < 1:
var r T
return r, false
case pos-1 == 0:
return ll.DelAtBeg()
case pos-1 == ll.Count():
case pos == ll.Count():
return ll.DelAtEnd()
}

Expand Down Expand Up @@ -160,6 +159,7 @@ func (ll *Singly[T]) ReversePartition(left, right int) error {
ll.Head = tmpNode.Next
return nil
}

func (ll *Singly[T]) CheckRangeFromIndex(left, right int) error {
if left > right {
return errors.New("left boundary must smaller than right")
Expand Down
27 changes: 25 additions & 2 deletions structure/linkedlist/singlylinkedlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,32 @@ func TestSingly(t *testing.T) {
}
})

list.AddAtEnd(4)
list.AddAtEnd(6)
t.Run("Test DelByePos() failed", func(t *testing.T) {
want := any(0)
got, ok := list.DelByPos(5)
if ok {
t.Error("unexpected ok")
}
if got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})

t.Run("Test DelByePos() succes", func(t *testing.T) {
want := any(6)
got, ok := list.DelByPos(4)
if !ok {
t.Error("unexpected not-ok")
}
if got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})

t.Run("Test Count()", func(t *testing.T) {
want := 2
want := any(3)
got := list.Count()
if got != want {
t.Errorf("got: %v, want: %v", got, want)
Expand Down Expand Up @@ -98,7 +122,6 @@ func TestSingly(t *testing.T) {
want := []any{1, 5, 4, 3, 2, 6}
got := []any{}
err := list2.ReversePartition(2, 5)

if err != nil {
t.Errorf("Incorrect boundary conditions entered%v", err)
}
Expand Down