-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
34 lines (29 loc) · 820 Bytes
/
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
package laney
// Queue is a FIFO (First in first out) data structure implementation.
// It is based on a deque container and focuses its API on core
// functionalities: Enqueue, Dequeue, Head, Size, Empty. Every operations time complexity
// is O(1).
//
// As it is implemented using a Deque container, every operations
// over an instiated Queue are synchronized and safe for concurrent
// usage.
type Queue[T any] struct {
*Deque[T]
}
func NewQueue[T any]() *Queue[T] {
return &Queue[T]{
Deque: NewDeque[T](),
}
}
// Enqueue adds an item at the back of the queue
func (q *Queue[T]) Enqueue(item T) {
q.Prepend(item)
}
// Dequeue removes and returns the front queue item
func (q *Queue[T]) Dequeue() T {
return q.Pop()
}
// Head returns the front queue item
func (q *Queue[T]) Head() T {
return q.Last()
}