Skip to content

Commit

Permalink
Add some functions in the linkedlist directory (#341)
Browse files Browse the repository at this point in the history
* Add AccessNodesByLayer Function, which is used to access nodes layer by layer instead of printing the results as one line

* Update the case of the function name in the main function so that it can run normally

* Add ReversePartition() Function, which is used to reverse the linked list from the ath to the bth node

* Add test program for function Reverse() in doublylinkedlist_test.go

Co-authored-by: Yaolin_Li <[email protected]>
Co-authored-by: Andrii Siriak <[email protected]>
Co-authored-by: Rak Laptudirm <[email protected]>
  • Loading branch information
4 people authored Sep 9, 2021
1 parent 1f2df33 commit b2e79af
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
18 changes: 18 additions & 0 deletions structure/linkedlist/doubly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,22 @@ func TestDoubly(t *testing.T) {
}
})

newList2 := NewDoubly()
newList2.AddAtBeg(1)
newList2.AddAtBeg(2)
newList2.AddAtBeg(3)
t.Run("Test Reverse", func(t *testing.T) {
want := []int{1, 2, 3}
got := []int{}
newList2.Reverse()
current := newList2.Head
got = append(got, current.Val.(int))
for current.Next != nil {
current = current.Next
got = append(got, current.Val.(int))
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})
}
38 changes: 37 additions & 1 deletion structure/linkedlist/singlylinkedlist.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package linkedlist

// demonstration of singly linked list in golang
import "fmt"
import (
"errors"
"fmt"
)

// Singly structure with length of the list and its head
type Singly struct {
Expand Down Expand Up @@ -97,6 +100,39 @@ func (ll *Singly) Reverse() {
ll.Head = prev
}

// ReversePartition Reverse the linked list from the ath to the bth node
func (ll *Singly) ReversePartition(left, right int) error {
err := ll.CheckRangeFromIndex(left, right)
if err != nil {
return err
}
tmpNode := NewNode(-1)
tmpNode.Next = ll.Head
pre := tmpNode
for i := 0; i < left-1; i++ {
pre = pre.Next
}
cur := pre.Next
for i := 0; i < right-left; i++ {
next := cur.Next
cur.Next = next.Next
next.Next = pre.Next
pre.Next = next
}
ll.Head = tmpNode.Next
return nil
}
func (ll *Singly) CheckRangeFromIndex(left, right int) error {
if left > right {
return errors.New("left boundary must smaller than right")
} else if left < 1 {
return errors.New("left boundary starts from the first node")
} else if right > ll.length {
return errors.New("right boundary cannot be greater than the length of the linked list")
}
return nil
}

// Display prints out the elements of the list.
func (ll *Singly) Display() {
for cur := ll.Head; cur != nil; cur = cur.Next {
Expand Down
42 changes: 42 additions & 0 deletions structure/linkedlist/singlylinkedlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,46 @@ func TestSingly(t *testing.T) {
t.Errorf("got: %v, want: %v", got, want)
}
})

list2 := Singly{}
list2.AddAtBeg(1)
list2.AddAtBeg(2)
list2.AddAtBeg(3)
list2.AddAtBeg(4)
list2.AddAtBeg(5)
list2.AddAtBeg(6)

t.Run("Test Reverse()", func(t *testing.T) {
want := []interface{}{1, 2, 3, 4, 5, 6}
got := []interface{}{}
list2.Reverse()
current := list2.Head
got = append(got, current.Val)
for current.Next != nil {
current = current.Next
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})

t.Run("Test ReversePartition()", func(t *testing.T) {
want := []interface{}{1, 5, 4, 3, 2, 6}
got := []interface{}{}
err := list2.ReversePartition(2, 5)

if err != nil {
t.Errorf("Incorrect boundary conditions entered%v", err)
}
current := list2.Head
got = append(got, current.Val)
for current.Next != nil {
current = current.Next
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})
}

0 comments on commit b2e79af

Please sign in to comment.