/
queue.go
80 lines (65 loc) · 1.29 KB
/
queue.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 2017 Jeff Foley. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package utils
import (
"sync"
)
type queueNode struct {
Next *queueNode
Data interface{}
}
// Queue implements a FIFO data structure.
type Queue struct {
sync.Mutex
size int
head, tail *queueNode
}
// NewQueue returns a Queue FIFO data structure.
func NewQueue() *Queue {
return new(Queue)
}
// Append adds the data to the end of the Queue.
func (q *Queue) Append(data interface{}) {
element := new(queueNode)
q.Lock()
defer q.Unlock()
q.size++
if q.head == nil {
q.head = element
}
end := q.tail
if end != nil {
end.Next = element
}
q.tail = element
element.Data = data
}
// Next returns the data at the front of the Queue.
func (q *Queue) Next() (interface{}, bool) {
q.Lock()
defer q.Unlock()
if q.head == nil {
return nil, false
}
q.size--
element := q.head
q.head = element.Next
if q.tail == element {
q.tail = nil
}
element.Next = nil
return element.Data, true
}
// Empty return true if the Queue is empty.
func (q *Queue) Empty() bool {
q.Lock()
defer q.Unlock()
if q.head == nil {
return true
}
return false
}
// Len returns the current length of the Queue
func (q *Queue) Len() int {
return q.size
}