-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.go
More file actions
248 lines (229 loc) · 4.62 KB
/
Copy pathLinkedList.go
File metadata and controls
248 lines (229 loc) · 4.62 KB
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
package LinkedList
// Value is LinkedList's value struct.
type Value[T interface{}] struct {
next *Value[T]
last *Value[T]
v T
}
// LinkedList is a thread unsafe two-way loop linked list.
type LinkedList[T interface{}] struct {
start *Value[T]
end *Value[T]
now *Value[T]
length int
index int
}
// Start gets the value of the first element.
func (ll *LinkedList[T]) Start() T {
return ll.start.v
}
// End gets the value of the last element.
func (ll *LinkedList[T]) End() T {
return ll.end.v
}
// Get the value of the current element.
func (ll *LinkedList[T]) Get() T {
return ll.now.v
}
// Set the value of the current element.
func (ll *LinkedList[T]) Set(v T) {
if ll.now == nil || ll.length == 0 {
panic("the LinkedList's length is 0")
}
ll.now.v = v
}
// IsStart checks if it is the first element.
func (ll *LinkedList[T]) IsStart() bool {
if ll.index == 0 {
return true
} else {
return false
}
}
// IsEnd checks if it is the last element.
func (ll *LinkedList[T]) IsEnd() bool {
if ll.index == ll.length-1 {
return true
} else {
return false
}
}
// Forward to the next element.
func (ll *LinkedList[T]) Forward() {
ll.now = ll.now.next
if ll.index == ll.length-1 {
ll.index = 0
} else {
ll.index += 1
}
}
// Back to the last element.
func (ll *LinkedList[T]) Back() {
ll.now = ll.now.last
if ll.index == 0 {
ll.index = ll.length - 1
} else {
ll.index -= 1
}
}
// Length get the LinkedList's length.
func (ll *LinkedList[T]) Length() int {
return ll.length
}
// Index get the current index.
func (ll *LinkedList[T]) Index() int {
return ll.index
}
// Insert a value before this element
func (ll *LinkedList[T]) Insert(v T) {
appe := &Value[T]{
next: ll.now,
last: ll.now.last,
v: v,
}
if ll.now == nil {
ll.now = appe
appe.next = appe
appe.last = appe
ll.end = appe
ll.start = appe
}
now := ll.now
las := ll.now.last
now.last = appe
las.next = appe
if now == ll.start {
ll.start = appe
}
ll.index += 1
ll.length += 1
}
// Append a value to the end of the LinkedList.
func (ll *LinkedList[T]) Append(v T) {
appe := &Value[T]{
last: ll.end,
next: ll.start,
v: v,
}
if ll.now == nil {
ll.now = appe
appe.next = appe
appe.last = appe
ll.end = appe
ll.start = appe
}
end := ll.end
sta := ll.start
end.next = appe
sta.last = appe
ll.end = appe
ll.length += 1
}
// Delete the current value and back to the last value.
func (ll *LinkedList[T]) Delete() {
now := ll.now
las := now.last
nex := now.next
las.next = nex
nex.last = las
if now == ll.end {
ll.end = las
}
if now == ll.start {
ll.start = nex
}
ll.length -= 1
ll.now = las
ll.index -= 1
}
// Positions:Some positions.
const (
// START represents the first value of the LinkedList.
START = iota
// END represents the last value's pointer of the LinkedList.
END
// BEFORE represents the value before 'now'.
BEFORE
// AFTER represents the value after 'now'.
AFTER
)
// Add the value to the item corresponding to position.
func (ll *LinkedList[T]) InsertTo(pos int, v T) {
if pos == START {
appe := &Value[T]{
last: ll.end,
next: ll.start,
v: v,
}
ll.end.next = appe
ll.start.last = appe
ll.start = appe
ll.length += 1
ll.index += 1
} else if pos == END {
appe := &Value[T]{
last: ll.end,
next: ll.start,
v: v,
}
ll.end.next = appe
ll.start.last = appe
ll.end = appe
ll.length += 1
} else if pos == BEFORE {
appe := &Value[T]{
last: ll.now.last,
next: ll.now,
v: v,
}
ll.now.last.next = appe
ll.now.last = appe
if ll.now == ll.start {
ll.start = appe
}
ll.length += 1
ll.index += 1
} else if pos == AFTER {
appe := &Value[T]{
last: ll.now,
next: ll.now.next,
v: v,
}
ll.now.next.last = appe
ll.now.next = appe
if ll.now == ll.end {
ll.end = appe
}
ll.length += 1
}
}
// Goto the item corresponding to the position(in var 'START','END','BEFORE' and 'AFTER').
func (ll *LinkedList[T]) Goto(pos int) {
if pos == START {
ll.now = ll.start
ll.index = 0
} else if pos == END {
ll.now = ll.end
ll.index = ll.length - 1
} else if pos == BEFORE {
ll.now = ll.now.last
ll.index -= 1
} else if pos == AFTER {
ll.now = ll.now.next
ll.index += 1
}
}
// Get the value of the item by selecting an position(in var 'START','END','BEFORE' and 'AFTER').
func (ll *LinkedList[T]) GetByPosition(pos int) T {
if pos == START {
return ll.start.v
} else if pos == END {
return ll.end.v
} else if pos == BEFORE {
return ll.now.last.v
} else if pos == AFTER {
return ll.now.next.v
} else {
panic("could not find this position, please select one in 'START','END','BEFORE' and 'AFTER'")
}
}