-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked-Lists_test.go
163 lines (130 loc) · 2.75 KB
/
Linked-Lists_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package sandbox
import (
"testing"
"github.com/stretchr/testify/assert"
)
type ListNode[T comparable] struct {
data T
next *ListNode[T]
}
type LinkedList[T comparable] struct {
head *ListNode[T]
}
func NewLinkedList[T comparable]() *LinkedList[T] {
l := &LinkedList[T]{}
return l
}
func (list *LinkedList[T]) Add(data T) {
new := &ListNode[T]{
data: data,
next: nil,
}
if list.head == nil {
list.head = new
return
}
current := list.head
for current.next != nil {
current = current.next
}
current.next = new
}
func (list *LinkedList[T]) Traverse(f func(*ListNode[T])) {
if list.head == nil {
return
}
current := list.head
f(current)
for current.next != nil {
current = current.next
f(current)
}
}
func (list *LinkedList[T]) Size() int {
size := 0
if list.head == nil {
return size
}
list.Traverse(func(*ListNode[T]) {
size++
})
return size
}
func Test_AddTraverse(t *testing.T) {
list := NewLinkedList[string]()
list.Add("A")
list.Add("B")
list.Add("C")
data := []string{}
list.Traverse(func(node *ListNode[string]) {
// fmt.Println(node.data)
data = append(data, node.data)
})
assert.Equal(t, []string{"A", "B", "C"}, data)
}
func Load[T comparable](a []T) *LinkedList[T] {
list := NewLinkedList[T]()
for _, v := range a {
list.Add(v)
}
return list
}
func (list *LinkedList[T]) Save() []T {
a := make([]T, 0)
list.Traverse(func(node *ListNode[T]) {
a = append(a, node.data)
})
return a
}
func Test_LoadSave(t *testing.T) {
input := []string{"A", "B", "C"}
list := Load(input)
output := list.Save()
assert.Equal(t, output, input)
}
func (list *LinkedList[T]) Delete(value T) (result bool) {
if list.head == nil {
return false
}
if list.head.data == value {
list.head = list.head.next
return true
}
prev := list.head
list.Traverse(func(node *ListNode[T]) {
if node.data == value {
prev.next = node.next
result = true
}
prev = node
})
return result
}
func Test_LoadDelete(t *testing.T) {
// Middle
list := Load([]string{"A", "B", "C"})
res := list.Delete("B")
output := list.Save()
assert.True(t, res, "delete middle result")
assert.Equal(t, []string{"A", "C"}, output, "delete middle")
// Last
list = Load([]string{"A", "B", "C"})
res = list.Delete("C")
assert.True(t, res, "delete last result")
assert.Equal(t, []string{"A", "B"}, list.Save(), "delete last")
// First
list = Load([]string{"A", "B", "C"})
res = list.Delete("A")
assert.True(t, res, "delete first result")
assert.Equal(t, []string{"B", "C"}, list.Save(), "delete first")
}
func Test_LoadSizeDelete(t *testing.T) {
// Middle
list := Load([]string{"A", "B", "C"})
assert.Equal(t, 3, list.Size())
list.Delete("B")
assert.Equal(t, 2, list.Size())
}
// ToDo:
// Create list with a cycle
// Cycle detection