-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.go
209 lines (186 loc) · 4.21 KB
/
linked_list.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
/*
* Copyright (c) 2022-2023 Lynn <lynnplus90@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gotypes
var (
_ Array[int] = (*LinkedList[int])(nil)
)
type listElement[T comparable] struct {
value T
prev *listElement[T]
next *listElement[T]
}
type LinkedList[T comparable] struct {
first *listElement[T]
last *listElement[T]
size int
}
// NewLinkedList return an LinkedList
func NewLinkedList[T comparable](values ...T) *LinkedList[T] {
list := &LinkedList[T]{}
list.Add(values...)
return list
}
func (list *LinkedList[T]) Add(values ...T) {
for _, value := range values {
newElement := &listElement[T]{value: value, prev: list.last}
if list.size == 0 {
list.first = newElement
list.last = newElement
} else {
list.last.next = newElement
list.last = newElement
}
list.size++
}
}
func (list *LinkedList[T]) Set(index int, v T) {
if !list.checkInRange(index) {
if index == list.size {
list.Add(v)
}
return
}
var temp *listElement[T]
if index > list.size-index {
temp = list.last
for i := list.size - 1; i != index; i, temp = i-1, temp.prev {
}
} else {
temp = list.first
for i := 0; i != index; i, temp = i+1, temp.next {
}
}
temp.value = v
}
func (list *LinkedList[T]) Get(index int) (T, bool) {
if !list.checkInRange(index) {
return *new(T), false
}
if list.size-index < index {
element := list.last
for e := list.size - 1; e != index; e, element = e-1, element.prev {
}
return element.value, true
}
element := list.first
for e := 0; e != index; e, element = e+1, element.next {
}
return element.value, true
}
func (list *LinkedList[T]) Remove(index int) {
if !list.checkInRange(index) {
return
}
if list.size == 1 {
list.RemoveAll()
return
}
var temp *listElement[T]
if index > list.size-index {
temp = list.last
for i := list.size - 1; i != index; i, temp = i-1, temp.prev {
}
} else {
temp = list.first
for i := 0; i != index; i, temp = i+1, temp.next {
}
}
if temp == list.first {
list.first = temp.next
}
if temp == list.last {
list.last = temp.prev
}
if temp.prev != nil {
temp.prev.next = temp.next
}
if temp.next != nil {
temp.next.prev = temp.prev
}
temp = nil
list.size--
}
func (list *LinkedList[T]) RemoveAll() {
list.size = 0
list.first = nil
list.last = nil
}
func (list *LinkedList[T]) IndexOf(value T) int {
if list.size == 0 {
return -1
}
index := -1
list.Range(func(i int, v T) bool {
if v == value {
index = i
return false
}
return true
})
return index
}
func (list *LinkedList[T]) Range(f func(index int, value T) bool) {
for i, ele := 0, list.first; ele != nil; i, ele = i+1, ele.next {
if !f(i, ele.value) {
break
}
}
}
func (list *LinkedList[T]) ReverseRange(f func(index int, value T) bool) {
for i, ele := list.size-1, list.last; ele != nil; i, ele = i-1, ele.prev {
if !f(i, ele.value) {
break
}
}
}
func (list *LinkedList[T]) Each(f func(index int, value T)) {
list.Range(func(i int, v1 T) bool {
f(i, v1)
return true
})
}
func (list *LinkedList[T]) Every(f func(index int, value T) bool) bool {
ok := true
list.Range(func(i int, v1 T) bool {
ok = f(i, v1)
return ok
})
return ok
}
func (list *LinkedList[T]) Some(f func(index int, value T) bool) bool {
ok := true
list.Range(func(i int, v1 T) bool {
ok = f(i, v1)
return !ok
})
return ok
}
func (list *LinkedList[T]) Values() []T {
values := make([]T, list.size, list.size)
list.Each(func(index int, value T) {
values[index] = value
})
return values
}
func (list *LinkedList[T]) Empty() bool {
return list.size == 0
}
func (list *LinkedList[T]) Size() int {
return list.size
}
func (list *LinkedList[T]) checkInRange(index int) bool {
return index >= 0 && index < list.size
}