-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqueue.go
More file actions
40 lines (33 loc) · 702 Bytes
/
queue.go
File metadata and controls
40 lines (33 loc) · 702 Bytes
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
package queue
// Queue is a queue.
type Queue[T any] struct {
list []T
nSize int
}
// NewQueue returns a new queue.
func NewQueue[T any]() *Queue[T] {
return &Queue[T]{
list: make([]T, 0),
}
}
// Enqueue adds a new node to the end of the queue.
func (q *Queue[T]) Enqueue(value T) {
q.list = append(q.list, value)
q.nSize++
}
// Dequeue removes the first node from the queue.
func (q *Queue[T]) Dequeue() {
if q.nSize == 0 {
return
}
q.list = q.list[1:]
q.nSize--
}
// Peek returns the value of the first node in the queue.
func (q *Queue[T]) Peek() T {
return q.list[0]
}
// IsEmpty returns true if the queue is empty.
func (q *Queue[T]) IsEmpty() bool {
return q.nSize == 0
}