-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueue.go
77 lines (62 loc) · 1.47 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
package datastructures
import (
dblList "container/list"
"errors"
"sync"
)
// EmptyQueueError is the error when a queue is unexpectedly empty
var ErrEmptyQueue = errors.New("Queue is empty!")
// Queue is a thread-safe FIFO queue using a doubly linked list for O(1) operations
type Queue struct {
list *dblList.List
mu *sync.RWMutex
}
// NewQueue creates an empty queue
func NewQueue() *Queue {
return &Queue{list: dblList.New(), mu: &sync.RWMutex{}}
}
// Enqueue adds an element to the back of the queue.
// Time O(1)
// Space O(1)
func (q *Queue) Enqueue(v interface{}) {
q.mu.Lock()
defer q.mu.Unlock()
q.list.PushBack(v)
}
// Dequeue removes and returns front element. EmptyQueueError if empty.
// Time O(1)
// Space O(1)
func (q *Queue) Dequeue() (interface{}, error) {
q.mu.Lock()
defer q.mu.Unlock()
if q.list.Len() == 0 {
return nil, ErrEmptyQueue
}
return q.list.Remove(q.list.Front()), nil
}
// Peek gives the front element without modifying the queue.
// EmptyQueueError if empty.
// Time O(1)
// Space O(1)
func (q *Queue) Peek() (interface{}, error) {
q.mu.RLock()
defer q.mu.RUnlock()
if q.list.Len() == 0 {
return nil, ErrEmptyQueue
}
return q.list.Back().Value, nil
}
// IsEmpty is true if there's nothing in the queue
// Time O(1)
// Space O(1)
func (q *Queue) IsEmpty() bool {
return q.Len() == 0
}
// Len gives the length of this queue
// Time O(1)
// Space O(1)
func (q *Queue) Len() int {
q.mu.RLock()
defer q.mu.RUnlock()
return q.list.Len()
}