/
linked.go
291 lines (268 loc) · 5.29 KB
/
linked.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package list
// LinkedList is doubly linked list
type LinkedList struct {
first *node
last *node
size int
}
type node struct {
val interface{}
prev *node
next *node
}
// Add adds value to the tail
func (list *LinkedList) Add(val interface{}) {
if list == nil {
panic("list is nil")
}
n := &node{
val: val,
}
if list.last == nil {
// empty list
list.first = n
list.last = n
} else {
n.prev = list.last
list.last.next = n
list.last = n
}
list.size++
}
func (list *LinkedList) find(index int) (n *node) {
if index < list.size/2 {
n = list.first
for i := 0; i < index; i++ {
n = n.next
}
} else {
n = list.last
for i := list.size - 1; i > index; i-- {
n = n.prev
}
}
return n
}
// Get returns value at the given index
func (list *LinkedList) Get(index int) (val interface{}) {
if list == nil {
panic("list is nil")
}
if index < 0 || index >= list.size {
panic("index out of bound")
}
return list.find(index).val
}
// Set updates value at the given index, the index should between [0, list.size]
func (list *LinkedList) Set(index int, val interface{}) {
if list == nil {
panic("list is nil")
}
if index < 0 || index > list.size {
panic("index out of bound")
}
n := list.find(index)
n.val = val
}
// Insert inserts value at the given index, the original element at the given index will move backward
func (list *LinkedList) Insert(index int, val interface{}) {
if list == nil {
panic("list is nil")
}
if index < 0 || index > list.size {
panic("index out of bound")
}
if index == list.size {
list.Add(val)
return
}
// list is not empty
pivot := list.find(index)
n := &node{
val: val,
prev: pivot.prev,
next: pivot,
}
if pivot.prev == nil {
list.first = n
} else {
pivot.prev.next = n
}
pivot.prev = n
list.size++
}
func (list *LinkedList) removeNode(n *node) {
if n.prev == nil {
list.first = n.next
} else {
n.prev.next = n.next
}
if n.next == nil {
list.last = n.prev
} else {
n.next.prev = n.prev
}
// for gc
n.prev = nil
n.next = nil
list.size--
}
// Remove removes value at the given index
func (list *LinkedList) Remove(index int) (val interface{}) {
if list == nil {
panic("list is nil")
}
if index < 0 || index >= list.size {
panic("index out of bound")
}
n := list.find(index)
list.removeNode(n)
return n.val
}
// RemoveLast removes the last element and returns its value
func (list *LinkedList) RemoveLast() (val interface{}) {
if list == nil {
panic("list is nil")
}
if list.last == nil {
// empty list
return nil
}
n := list.last
list.removeNode(n)
return n.val
}
// RemoveAllByVal removes all elements with the given val
func (list *LinkedList) RemoveAllByVal(expected Expected) int {
if list == nil {
panic("list is nil")
}
n := list.first
removed := 0
var nextNode *node
for n != nil {
nextNode = n.next
if expected(n.val) {
list.removeNode(n)
removed++
}
n = nextNode
}
return removed
}
// RemoveByVal removes at most `count` values of the specified value in this list
// scan from left to right
func (list *LinkedList) RemoveByVal(expected Expected, count int) int {
if list == nil {
panic("list is nil")
}
n := list.first
removed := 0
var nextNode *node
for n != nil {
nextNode = n.next
if expected(n.val) {
list.removeNode(n)
removed++
}
if removed == count {
break
}
n = nextNode
}
return removed
}
// ReverseRemoveByVal removes at most `count` values of the specified value in this list
// scan from right to left
func (list *LinkedList) ReverseRemoveByVal(expected Expected, count int) int {
if list == nil {
panic("list is nil")
}
n := list.last
removed := 0
var prevNode *node
for n != nil {
prevNode = n.prev
if expected(n.val) {
list.removeNode(n)
removed++
}
if removed == count {
break
}
n = prevNode
}
return removed
}
// Len returns the number of elements in list
func (list *LinkedList) Len() int {
if list == nil {
panic("list is nil")
}
return list.size
}
// ForEach visits each element in the list
// if the consumer returns false, the loop will be break
func (list *LinkedList) ForEach(consumer Consumer) {
if list == nil {
panic("list is nil")
}
n := list.first
i := 0
for n != nil {
goNext := consumer(i, n.val)
if !goNext {
break
}
i++
n = n.next
}
}
// Contains returns whether the given value exist in the list
func (list *LinkedList) Contains(expected Expected) bool {
contains := false
list.ForEach(func(i int, actual interface{}) bool {
if expected(actual) {
contains = true
return false
}
return true
})
return contains
}
// Range returns elements which index within [start, stop)
func (list *LinkedList) Range(start int, stop int) []interface{} {
if list == nil {
panic("list is nil")
}
if start < 0 || start >= list.size {
panic("`start` out of range")
}
if stop < start || stop > list.size {
panic("`stop` out of range")
}
sliceSize := stop - start
slice := make([]interface{}, sliceSize)
n := list.first
i := 0
sliceIndex := 0
for n != nil {
if i >= start && i < stop {
slice[sliceIndex] = n.val
sliceIndex++
} else if i >= stop {
break
}
i++
n = n.next
}
return slice
}
// Make creates a new linked list
func Make(vals ...interface{}) *LinkedList {
list := LinkedList{}
for _, v := range vals {
list.Add(v)
}
return &list
}