-
Notifications
You must be signed in to change notification settings - Fork 1
/
linked_list.go
196 lines (166 loc) · 3.52 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
package list
import "sync"
// Node is a linked list object item
type Node[T any] struct {
val T
lf, rg *Node[T]
}
// Linked struct represents linked list
type Linked[T any] struct {
head *Node[T]
tail *Node[T]
len int
mx locker
}
// NewSyncLinked creates a new instance of a synchronized linked list
func NewSyncLinked[T any]() Linked[T] {
return Linked[T]{mx: locker{&sync.Mutex{}}}
}
// PushBack adds a new node with the given value at the end of the list
func (list *Linked[T]) PushBack(val T) {
list.mx.Lock()
defer list.mx.Unlock()
newNode := &Node[T]{val: val}
if list.tail != nil {
list.tail.rg = newNode
newNode.lf = list.tail
} else {
list.head = newNode
}
list.tail = newNode
list.len++
}
// PushFront adds a new node with the given value at the beginning of the list
func (list *Linked[T]) PushFront(val T) {
list.mx.Lock()
defer list.mx.Unlock()
newNode := &Node[T]{val: val}
if list.head == nil {
// inserting the first element
list.head = newNode
list.tail = newNode
list.len = 1
return
}
newNode.rg = list.head
list.head.lf = newNode
list.head = newNode
list.len++
}
// PopBack removes the last node from the list and returns its value and a boolean indicating success
func (list *Linked[T]) PopBack() (T, bool) {
list.mx.Lock()
defer list.mx.Unlock()
if list.tail == nil {
var zero T
return zero, false
}
val := list.tail.val
list.tail = list.tail.lf
if list.tail == nil {
// it is the last element
list.head = nil
list.len = 0
return val, true
}
list.tail.rg = nil
list.len--
return val, true
}
// PopFront removes the first node and returns its value and boolean indicating success
func (list *Linked[T]) PopFront() (T, bool) {
list.mx.Lock()
defer list.mx.Unlock()
if list.head == nil {
var zero T
return zero, false
}
val := list.head.val
list.head = list.head.rg
if list.head != nil {
list.head.lf = nil
list.len--
return val, true
}
list.len = 0
list.tail = nil
return val, true
}
// Head returns the first node of the list
func (list *Linked[T]) Head() *Node[T] {
list.mx.Lock()
head := list.head
list.mx.Unlock()
return head
}
// Tail returns the last node of the list
func (list *Linked[T]) Tail() *Node[T] {
list.mx.Lock()
tail := list.tail
list.mx.Unlock()
return tail
}
// Size returns the number of nodes in the list
func (list *Linked[T]) Size() int {
list.mx.Lock()
len := list.len
list.mx.Unlock()
return len
}
// Empty checks if the list is empty
func (list *Linked[T]) Empty() bool {
list.mx.Lock()
len := list.len
list.mx.Unlock()
return len == 0
}
// Begin iterates over the list from the head to the tail
func (list *Linked[T]) Begin(yield func(T) bool) {
cur := list.Head()
for cur != nil {
if !yield(cur.val) {
return
}
list.mx.Lock()
cur = cur.rg
list.mx.Unlock()
}
}
// End iterates over the list from the tail to the head
func (list *Linked[T]) End(yield func(T) bool) {
cur := list.Tail()
for cur != nil {
if !yield(cur.val) {
return
}
list.mx.Lock()
cur = cur.lf
list.mx.Unlock()
}
}
// Val returns the value of the node
func (node *Node[T]) Val() T {
return node.val
}
// Next returns the next node
func (node *Node[T]) Next() *Node[T] {
return node.rg
}
// Prev returns the previous node
func (node *Node[T]) Prev() *Node[T] {
return node.lf
}
// locker is an internal object to support both sync and unsync lists
type locker struct {
*sync.Mutex
}
func (l locker) Lock() {
if l.Mutex != nil {
l.Mutex.Lock()
}
}
func (l locker) Unlock() {
if l.Mutex != nil {
l.Mutex.Unlock()
}
}