forked from kokizzu/goadsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglyLinearLinkedList.go
345 lines (302 loc) · 7.72 KB
/
SinglyLinearLinkedList.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
Singly Linked List Interface
*/
package main
import "fmt"
type Any interface{}
type Node struct {
Data Any
NextLink *Node
}
func (n *Node) GetData() Any {
return n.Data
}
func (n *Node) SetData(data Any) {
n.Data = data
}
func (n *Node) GetNext() *Node {
return n.NextLink
}
func (n *Node) SetNext(next *Node) {
n.NextLink = next
}
func (n *Node) HasNext() bool {
if n.NextLink == nil { return false }
return true
}
type SLL struct {
Head *Node
count int
}
func NewSLL() *SLL {
return &SLL{}
}
func (s *SLL) Count() int {
return s.count
}
// Insert to Head: O(1)
func (s *SLL) InsertFirst(newnode *Node) int {
s.count++
oldhead := s.Head
newnode.SetNext(oldhead)
s.Head = newnode
return 0
}
// Insert to Tail: O(N)
func (s *SLL) InsertLast(newnode *Node) int {
s.count++
if s.Head == nil {
s.Head = newnode
return 0
}
current := s.Head
for current.GetNext() != nil {
current = current.GetNext()
}
current.SetNext(newnode)
return s.count-1
}
// Insert to Nth Node: O(N)
func (s *SLL) InsertNth(newnode *Node, n int) int {
s.count++
if s.Head == nil {
s.Head = newnode
return 0
}
pos := 0
current := s.Head
var prev *Node
for (current.GetNext() != nil) && (pos < n) {
prev = current
current = current.GetNext()
pos++
}
if pos == 0 {
oldhead := current
newnode.SetNext(oldhead)
s.Head = newnode
} else if pos != n {
current.SetNext(newnode)
} else {
prev.SetNext(newnode)
newnode.SetNext(current)
}
return pos
}
// Delete Head: O(1)
func (s *SLL) DeleteFirst() {
if s.Head != nil {
s.count--
s.Head = s.Head.GetNext()
}
}
// Delete Tail: O(N)
func (s *SLL) DeleteLast() {
if s.Head != nil {
s.count--
current := s.Head
var prev *Node
for current.GetNext() != nil {
prev = current
current = current.GetNext()
}
if prev == nil {
s.Head = nil
} else {
prev.SetNext(nil)
}
}
}
// Delete Nth Node: O(N)
func (s *SLL) DeleteNth(n int) {
if s.Head != nil {
s.count--
current := s.Head
var prev *Node
pos := 0
for (current.GetNext() != nil) && (pos < n) {
prev = current
current = current.GetNext()
pos++
}
if pos == 0 {
s.Head = current.GetNext()
} else if pos != n {
prev.SetNext(nil)
} else {
prev.SetNext(current.GetNext())
}
}
}
// Delete All: O(1)
func (s *SLL) DeleteAll() {
s.count = 0
s.Head = nil
}
// Move Nth Node to Head: O(N)
func (s *SLL) MoveNthToFirst(n int) {
if (s.Head != nil) && (n < s.count) {
current := s.Head
var prev *Node
pos := 0
for (current.GetNext() != nil) && (pos < n) {
prev = current
current = current.GetNext()
pos++
}
if pos > 0 {
next := current.GetNext()
oldhead := s.Head
current.SetNext(oldhead)
s.Head = current
prev.SetNext(next)
}
}
}
// Move Nth Node to Tail: O(N)
func (s *SLL) MoveNthToLast(n int) {
if (s.Head != nil) && (n < s.count) {
current := s.Head
var prev *Node
pos := 0
for (current.GetNext() != nil) && (pos < n) {
prev = current
current = current.GetNext()
pos++
}
lastnode := current
for lastnode.GetNext() != nil {
lastnode = lastnode.GetNext()
}
if current.GetNext() != nil {
next := current.GetNext()
lastnode.SetNext(current)
current.SetNext(nil)
if prev == nil {
s.Head = next
} else {
prev.SetNext(next)
}
}
}
}
// Move Nth Node to Nth Position: O(N)
func (s *SLL) MoveNthToNth(src, dest int) {
if (s.Head != nil) && (src < s.count) && (dest < s.count) && (src != dest) {
current := s.Head
var srcnode, srcprev, destnode, destprev, prev *Node
pos := 0
for current != nil {
if pos == src {
srcnode = current
srcprev = prev
}
if pos == dest {
destnode = current
destprev = prev
}
prev = current
current = current.GetNext()
pos++
}
if src < dest {
if srcprev == nil {
s.Head = srcnode.GetNext()
} else {
srcprev.SetNext(srcnode.GetNext())
}
srcnode.SetNext(destnode.GetNext())
destnode.SetNext(srcnode)
} else {
srcprev.SetNext(srcnode.GetNext())
srcnode.SetNext(destnode)
if destprev == nil {
s.Head = srcnode
} else {
destprev.SetNext(srcnode)
}
}
}
}
// Print From Head to Tail: O(N)
func (s *SLL) Print() {
if s.Head == nil {
fmt.Println("| Empty |")
return
}
current := s.Head
pos := 0
fmt.Print("|")
for current != nil {
fmt.Printf(" %d: %#v |", pos, current.Data)
current = current.GetNext()
pos++
}
fmt.Println()
}
func main() {
s := NewSLL()
fmt.Println("Kondisi Awal DLL:")
s.Print() // empty
fmt.Println()
fmt.Println("Masukan integer(1) dari depan:")
s.InsertFirst(&Node{1, nil}) // 1
s.Print()
fmt.Println()
fmt.Println("Masukan string hello dari depan:")
s.InsertFirst(&Node{"hello", nil}) // "hello" 1
s.Print()
fmt.Println()
fmt.Println("Masukan string # dari belakang:")
s.InsertLast(&Node{"#", nil}) // "hello" 1 "#"
s.Print()
fmt.Println()
fmt.Println("Masukan float 13.9 dari belakang:")
s.InsertLast(&Node{13.9, nil}) // "hello" 1 "#" 13.9
s.Print()
fmt.Println()
fmt.Println("Masukan integer(14) ke posisi N yaitu 4(3, index mulai dari 0):")
s.InsertNth(&Node{14, nil},3) // "hello" 1 "#" 14 13.9
s.Print()
fmt.Println()
fmt.Println("Hapus Head:")
s.DeleteFirst() // 1 "#" 14 13.9
s.Print()
fmt.Println()
fmt.Println("Hapus Tail:")
s.DeleteLast() // 1 "#" 14
s.Print()
fmt.Println()
fmt.Println("Hapus Posisi N yaitu 2(1, index mulai dari 0):")
s.DeleteNth(1) // 1 14
s.Print()
fmt.Println()
fmt.Println("Hapus semuanya:")
s.DeleteAll() // empty
s.Print()
fmt.Println()
fmt.Println("Masukan 5 integer dari belakang(tail):")
for i := 0; i < 5; i++ {
s.InsertLast(&Node{i, nil})
s.Print()
} // 0 1 2 3 4
fmt.Println()
fmt.Println("Kondisi saat ini:")
s.Print()
fmt.Println()
fmt.Println("Pindahkan posisi n yaitu 4(3, index mulai dari 0) ke depan:")
s.MoveNthToFirst(3) // 3 0 1 2 4
s.Print()
fmt.Println()
fmt.Println("Pindahkan posisi n yaitu 2(1, index mulai dari 0) ke tail/belakang:")
s.MoveNthToLast(1) // 3 1 2 4 0
s.Print()
fmt.Println()
fmt.Println("Pindahkan posisi n yaitu 3(2, index mulai dari 0) ke posisi n yaitu 5(4, index mulai dari 0):")
s.MoveNthToNth(2,4) // 3 1 4 0 2
s.Print()
fmt.Println()
fmt.Println("Pindahkan posisi n yaitu 4(3, index mulai dari 0) ke posisi n yaitu 2(1, index mulai dari 0):")
s.MoveNthToNth(3,1) // 3 0 1 4 2
s.Print()
}