Skip to content

Commit b2e79af

Browse files
yaolin-LiYaolin_Lisiriakraklaptudirm
authored
Add some functions in the linkedlist directory (#341)
* 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]>
1 parent 1f2df33 commit b2e79af

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

structure/linkedlist/doubly_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,22 @@ func TestDoubly(t *testing.T) {
8585
}
8686
})
8787

88+
newList2 := NewDoubly()
89+
newList2.AddAtBeg(1)
90+
newList2.AddAtBeg(2)
91+
newList2.AddAtBeg(3)
92+
t.Run("Test Reverse", func(t *testing.T) {
93+
want := []int{1, 2, 3}
94+
got := []int{}
95+
newList2.Reverse()
96+
current := newList2.Head
97+
got = append(got, current.Val.(int))
98+
for current.Next != nil {
99+
current = current.Next
100+
got = append(got, current.Val.(int))
101+
}
102+
if !reflect.DeepEqual(got, want) {
103+
t.Errorf("got: %v, want: %v", got, want)
104+
}
105+
})
88106
}

structure/linkedlist/singlylinkedlist.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package linkedlist
22

33
// demonstration of singly linked list in golang
4-
import "fmt"
4+
import (
5+
"errors"
6+
"fmt"
7+
)
58

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

103+
// ReversePartition Reverse the linked list from the ath to the bth node
104+
func (ll *Singly) ReversePartition(left, right int) error {
105+
err := ll.CheckRangeFromIndex(left, right)
106+
if err != nil {
107+
return err
108+
}
109+
tmpNode := NewNode(-1)
110+
tmpNode.Next = ll.Head
111+
pre := tmpNode
112+
for i := 0; i < left-1; i++ {
113+
pre = pre.Next
114+
}
115+
cur := pre.Next
116+
for i := 0; i < right-left; i++ {
117+
next := cur.Next
118+
cur.Next = next.Next
119+
next.Next = pre.Next
120+
pre.Next = next
121+
}
122+
ll.Head = tmpNode.Next
123+
return nil
124+
}
125+
func (ll *Singly) CheckRangeFromIndex(left, right int) error {
126+
if left > right {
127+
return errors.New("left boundary must smaller than right")
128+
} else if left < 1 {
129+
return errors.New("left boundary starts from the first node")
130+
} else if right > ll.length {
131+
return errors.New("right boundary cannot be greater than the length of the linked list")
132+
}
133+
return nil
134+
}
135+
100136
// Display prints out the elements of the list.
101137
func (ll *Singly) Display() {
102138
for cur := ll.Head; cur != nil; cur = cur.Next {

structure/linkedlist/singlylinkedlist_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,46 @@ func TestSingly(t *testing.T) {
6464
t.Errorf("got: %v, want: %v", got, want)
6565
}
6666
})
67+
68+
list2 := Singly{}
69+
list2.AddAtBeg(1)
70+
list2.AddAtBeg(2)
71+
list2.AddAtBeg(3)
72+
list2.AddAtBeg(4)
73+
list2.AddAtBeg(5)
74+
list2.AddAtBeg(6)
75+
76+
t.Run("Test Reverse()", func(t *testing.T) {
77+
want := []interface{}{1, 2, 3, 4, 5, 6}
78+
got := []interface{}{}
79+
list2.Reverse()
80+
current := list2.Head
81+
got = append(got, current.Val)
82+
for current.Next != nil {
83+
current = current.Next
84+
got = append(got, current.Val)
85+
}
86+
if !reflect.DeepEqual(got, want) {
87+
t.Errorf("got: %v, want: %v", got, want)
88+
}
89+
})
90+
91+
t.Run("Test ReversePartition()", func(t *testing.T) {
92+
want := []interface{}{1, 5, 4, 3, 2, 6}
93+
got := []interface{}{}
94+
err := list2.ReversePartition(2, 5)
95+
96+
if err != nil {
97+
t.Errorf("Incorrect boundary conditions entered%v", err)
98+
}
99+
current := list2.Head
100+
got = append(got, current.Val)
101+
for current.Next != nil {
102+
current = current.Next
103+
got = append(got, current.Val)
104+
}
105+
if !reflect.DeepEqual(got, want) {
106+
t.Errorf("got: %v, want: %v", got, want)
107+
}
108+
})
67109
}

0 commit comments

Comments
 (0)