-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.go
75 lines (68 loc) · 1.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
package changroup
import "sync"
// list is a doubly linked list with limited number of methods.
type list[T any] struct {
mu sync.RWMutex
first *node[T]
last *node[T]
}
func newList[T any]() *list[T] {
return &list[T]{
mu: sync.RWMutex{},
first: nil,
last: nil,
}
}
// node is an element of doubly linked list.
type node[T any] struct {
elem T
prev *node[T]
next *node[T]
list *list[T]
}
// Append inserts new node to the end of the list.
func (l *list[T]) Append(elem T) *node[T] {
l.mu.Lock()
defer l.mu.Unlock()
n := &node[T]{
elem: elem,
prev: l.last,
next: nil,
list: l,
}
if l.last == nil {
l.first = n
} else {
l.last.next = n
}
l.last = n
return n
}
// Delete removes node from the list.
func (n *node[T]) Delete() {
n.list.mu.Lock()
defer n.list.mu.Unlock()
if n.list.first == n {
n.list.first = n.next
}
if n.list.last == n {
n.list.last = n.prev
}
if n.next != nil {
n.next.prev = n.prev
}
if n.prev != nil {
n.prev.next = n.next
}
n.next, n.prev = nil, nil
}
// ForEach loops over all elements and calls f with each of them.
func (l *list[T]) ForEach(f func(T)) {
l.mu.RLock()
defer l.mu.RUnlock()
n := l.first
for n != nil {
f(n.elem)
n = n.next
}
}