-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathlinkedlist.go
80 lines (65 loc) · 1.45 KB
/
linkedlist.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
// Copyright (c) Efficient Go Authors
// Licensed under the Apache License 2.0.
package prealloc
// Example of pre-allocation of linked list elements.
// Read more in "Efficient Go"; Example 11-14.
type Node struct {
next *Node
value int
}
type SinglyLinkedList struct {
head *Node
pool []Node
poolIndex int
}
func (l *SinglyLinkedList) Grow(len int) {
l.pool = make([]Node, len)
l.poolIndex = 0
}
func (l *SinglyLinkedList) Insert(value int) {
var newNode *Node
if len(l.pool) > l.poolIndex {
newNode = &l.pool[l.poolIndex]
l.poolIndex++
} else {
newNode = &Node{}
}
newNode.next = l.head
newNode.value = value
l.head = newNode
}
// Delete deletes node. However, this showcases kind-of leaking code.
// Read more in "Efficient Go"; Example 11-15.
func (l *SinglyLinkedList) Delete(n *Node) {
if l.head == n {
l.head = n.next
return
}
for curr := l.head; curr != nil; curr = curr.next {
if curr.next != n {
continue
}
curr.next = n.next
return
}
}
// ClipMemory releases unused memory.
// Read more in "Efficient Go"; Example 11-16.
func (l *SinglyLinkedList) ClipMemory() {
var objs int
for curr := l.head; curr != nil; curr = curr.next {
objs++
}
l.pool = make([]Node, objs)
l.poolIndex = 0
for curr := l.head; curr != nil; curr = curr.next {
oldCurr := curr
curr = &l.pool[l.poolIndex]
l.poolIndex++
curr.next = oldCurr.next
curr.value = oldCurr.value
if oldCurr == l.head {
l.head = curr
}
}
}