-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleQueue.go
59 lines (46 loc) · 982 Bytes
/
SimpleQueue.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
package queue
import (
"errors"
)
type SimpleQueue[T any] struct {
front int
rear int
capacity int
items []T
}
// CreateSimpleQueue initializes a new simple queue with a given capacity
func CreateSimpleQueue[T any](capacity int) *SimpleQueue[T] {
if capacity < 1 {
return nil
}
return &SimpleQueue[T]{
front: 0,
rear: -1,
capacity: capacity,
items: make([]T, 0, capacity),
}
}
func (q *SimpleQueue[T]) EnqueueSimple(item T) error {
if q.rear == q.capacity-1 {
return errors.New("queue is full")
}
q.rear++
q.items = append(q.items, item)
return nil
}
func (q *SimpleQueue[T]) DequeueSimple() (T, error) {
var zeroValue T // default value for type T
if q.IsEmpty() {
return zeroValue, errors.New("queue is empty")
}
item := q.items[0]
q.items = q.items[1:]
q.rear--
return item, nil
}
func (q *SimpleQueue[T]) IsEmpty() bool {
return q.front > q.rear
}
func (q *SimpleQueue[T]) Size() int {
return len(q.items)
}