1
1
package main
2
2
3
- import (
4
- "reflect"
5
- "testing"
6
- )
3
+ import "fmt"
7
4
8
5
type Node struct {
9
6
Value int
@@ -17,159 +14,152 @@ type SinglyLinkedList struct {
17
14
func (list * SinglyLinkedList ) AddLeft (value int ) {
18
15
node := Node {Value : value }
19
16
20
- if list .Head == nil {
21
- list .Head = & node
22
- return
23
- }
17
+ if list .Head == nil {
18
+ list .Head = & node
19
+ return
20
+ }
24
21
25
- node .Next = list .Head
26
- list .Head = & node
22
+ node .Next = list .Head
23
+ list .Head = & node
27
24
}
28
25
29
26
func (list * SinglyLinkedList ) AddRight (value int ) {
30
- node := Node {Value : value }
27
+ node := Node {Value : value }
31
28
32
- if list .Head == nil {
33
- list .Head = & node
34
- return
35
- }
29
+ if list .Head == nil {
30
+ list .Head = & node
31
+ return
32
+ }
36
33
37
- pointer := list .Head
34
+ pointer := list .Head
38
35
39
- for pointer .Next != nil {
40
- pointer = pointer .Next
41
- }
36
+ for pointer .Next != nil {
37
+ pointer = pointer .Next
38
+ }
42
39
43
- pointer .Next = & node
40
+ pointer .Next = & node
44
41
}
45
42
46
43
func (list * SinglyLinkedList ) RemoveLeft () {
47
- if list .Head == nil {
48
- return
49
- }
44
+ if list .Head == nil {
45
+ return
46
+ }
50
47
51
- list .Head = list .Head .Next
48
+ list .Head = list .Head .Next
52
49
}
53
50
54
51
func (list * SinglyLinkedList ) RemoveRight () {
55
52
if list .Head == nil {
56
- return
57
- }
53
+ return
54
+ }
58
55
59
- if list .Head .Next == nil {
60
- list .Head = nil
61
- return
62
- }
56
+ if list .Head .Next == nil {
57
+ list .Head = nil
58
+ return
59
+ }
63
60
64
- pointer := list .Head
61
+ pointer := list .Head
65
62
66
- for pointer .Next .Next != nil {
67
- pointer = pointer .Next
68
- }
63
+ for pointer .Next .Next != nil {
64
+ pointer = pointer .Next
65
+ }
69
66
70
- pointer .Next = nil
67
+ pointer .Next = nil
71
68
}
72
69
73
70
func (list * SinglyLinkedList ) FindAt (idx int ) * Node {
74
- pointer := list .Head
71
+ pointer := list .Head
75
72
76
- for i := 0 ; i < idx ; i ++ {
77
- pointer = pointer .Next
78
- if pointer == nil {
79
- return nil
80
- }
81
- }
73
+ for i := 0 ; i < idx ; i ++ {
74
+ pointer = pointer .Next
75
+ if pointer == nil {
76
+ return nil
77
+ }
78
+ }
82
79
83
- return pointer
80
+ return pointer
84
81
}
85
82
86
83
func (list * SinglyLinkedList ) AddAt (idx , value int ) {
87
- if idx == 0 {
88
- list .AddLeft (value )
89
- return
90
- }
84
+ if idx == 0 {
85
+ list .AddLeft (value )
86
+ return
87
+ }
91
88
92
- prevNode := list .FindAt (idx - 1 )
89
+ prevNode := list .FindAt (idx - 1 )
93
90
94
- if prevNode == nil {
95
- return
96
- }
91
+ if prevNode == nil {
92
+ return
93
+ }
97
94
98
- node := Node {Value : value }
95
+ node := Node {Value : value }
99
96
100
- node .Next = prevNode .Next
101
- prevNode .Next = & node
97
+ node .Next = prevNode .Next
98
+ prevNode .Next = & node
102
99
}
103
100
104
101
func (list * SinglyLinkedList ) RemoveAt (idx int ) {
105
- if idx == 0 {
106
- list .RemoveLeft ()
107
- return
108
- }
102
+ if idx == 0 {
103
+ list .RemoveLeft ()
104
+ return
105
+ }
109
106
110
- prevNode := list .FindAt (idx - 1 )
107
+ prevNode := list .FindAt (idx - 1 )
111
108
112
- if prevNode == nil {
113
- return
114
- }
109
+ if prevNode == nil {
110
+ return
111
+ }
115
112
116
- prevNode .Next = prevNode .Next .Next
113
+ prevNode .Next = prevNode .Next .Next
117
114
}
118
115
119
116
func (list * SinglyLinkedList ) ToSlice () []int {
120
- var slice []int
117
+ var slice []int
121
118
122
- for pointer := list .Head ; pointer != nil ; pointer = pointer .Next {
123
- slice = append (slice , pointer .Value )
124
- }
119
+ for pointer := list .Head ; pointer != nil ; pointer = pointer .Next {
120
+ slice = append (slice , pointer .Value )
121
+ }
125
122
126
- return slice
123
+ return slice
127
124
}
128
125
129
- func TestSinglyLinkedList ( t * testing. T ) {
126
+ func main ( ) {
130
127
list := SinglyLinkedList {}
131
128
132
- // test AddLeft
129
+ fmt . Println ( " AddLeft(1, 2)" )
133
130
list .AddLeft (1 )
134
131
list .AddLeft (2 )
135
- if ! reflect .DeepEqual (list .ToSlice (), []int {2 , 1 }) {
136
- t .Errorf ("AddLeft failed, expected %v, got %v" , []int {2 , 1 }, list .ToSlice ())
137
- }
138
132
139
- // test AddRight
133
+ fmt .Println ("list.toSlice(): " , list .ToSlice ())
134
+
135
+ fmt .Println ("AddRight(3, 4)" )
140
136
list .AddRight (3 )
141
137
list .AddRight (4 )
142
- if ! reflect .DeepEqual (list .ToSlice (), []int {2 , 1 , 3 , 4 }) {
143
- t .Errorf ("AddRight failed, expected %v, got %v" , []int {2 , 1 , 3 , 4 }, list .ToSlice ())
144
- }
145
138
146
- //// test RemoveLeft
139
+ fmt .Println ("list.toSlice(): " , list .ToSlice ())
140
+
141
+ fmt .Println ("RemoveLeft()" )
147
142
list .RemoveLeft ()
148
- if ! reflect .DeepEqual (list .ToSlice (), []int {1 , 3 , 4 }) {
149
- t .Errorf ("RemoveLeft failed, expected %v, got %v" , []int {1 , 3 , 4 }, list .ToSlice ())
150
- }
151
143
152
- // test RemoveRight
144
+ fmt .Println ("list.toSlice(): " , list .ToSlice ())
145
+
146
+ fmt .Println ("RemoveRight()" )
153
147
list .RemoveRight ()
154
- if ! reflect .DeepEqual (list .ToSlice (), []int {1 , 3 }) {
155
- t .Errorf ("RemoveRight failed, expected %v, got %v" , []int {1 , 3 }, list .ToSlice ())
156
- }
157
148
158
- // test AddAt
149
+ fmt .Println ("list.toSlice(): " , list .ToSlice ())
150
+
151
+ fmt .Println ("AddAt(1, 5)" )
159
152
list .AddAt (1 , 5 )
160
- if ! reflect .DeepEqual (list .ToSlice (), []int {1 , 5 , 3 }) {
161
- t .Errorf ("AddAt failed, expected %v, got %v" , []int {1 , 5 , 3 }, list .ToSlice ())
162
- }
163
153
164
- // test RemoveAt
154
+ fmt .Println ("list.toSlice(): " , list .ToSlice ())
155
+
156
+ fmt .Println ("RemoveAt(2)" )
165
157
list .RemoveAt (2 )
166
- if ! reflect .DeepEqual (list .ToSlice (), []int {1 , 5 }) {
167
- t .Errorf ("RemoveAt failed, expected %v, got %v" , []int {1 , 5 }, list .ToSlice ())
168
- }
169
158
170
- // test FindAt
159
+ fmt .Println ("list.toSlice(): " , list .ToSlice ())
160
+
161
+ fmt .Println ("FindAt(0)" )
171
162
node := list .FindAt (0 )
172
- if node == nil || node .Value != 1 {
173
- t .Errorf ("FindAt failed, expected %v, got %v" , 1 , node .Value )
174
- }
163
+ fmt .Println ("node.Value: " , node .Value )
164
+
175
165
}
0 commit comments