-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice_queue.go
61 lines (48 loc) · 1.29 KB
/
slice_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
package queue
import "fmt"
type SliceQueue[T any] struct {
data []T
}
// New constructs and returns an empty slice-queue.
// time-complexity: O(1)
func New[T any]() SliceQueue[T] {
return SliceQueue[T]{data: []T{}}
}
// Enqueue adds an element to the end of the queue.
// time-complexity: amortized O(1)
func (q *SliceQueue[T]) Enqueue(data T) {
q.data = append(q.data, data)
}
// Dequeue removes and returns the front element of the queue. It returns false if the queue was empty.
// time-complexity: O(1)
func (q *SliceQueue[T]) Dequeue() (val T, ok bool) {
if q.IsEmpty() {
return
}
val = q.data[0]
q.data = q.data[1:]
return val, true
}
// First returns the front element of the queue. It returns false if the queue was empty.
// time-complexity: O(1)
func (q *SliceQueue[T]) First() (val T, ok bool) {
if q.IsEmpty() {
return
}
return q.data[0], true
}
// Size returns the number of the elements in the queue.
// time-complexity: O(1)
func (q *SliceQueue[T]) Size() int {
return len(q.data)
}
// IsEmpty returns true if the queue is empty.
// time-complexity: O(1)
func (q *SliceQueue[T]) IsEmpty() bool {
return q.Size() == 0
}
// String returns the string representation of the queue.
// time-complexity: O(n)
func (q *SliceQueue[T]) String() string {
return fmt.Sprint(q.data)
}