-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathqueue.go
51 lines (39 loc) · 974 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package ch03
import "errors"
// Queue data structure implementation based on Linked List
type Queue[T any] struct {
first *Item[T]
last *Item[T]
}
// Enqueue adds an item to the beggining of the queue
func (q *Queue[T]) Enqueue(data T) {
item := New(data)
if q.last != nil {
q.last.next = item
}
// Same as q.last = q.last.next
q.last = item
if q.first == nil {
q.first = q.last
}
}
// Dequeue returns (and removes) an item from the queue
func (q *Queue[T]) Dequeue() (T, error) {
if q.IsEmpty() {
return *new(T), errors.New("empty Queue error")
}
data := q.first.data
q.first = q.first.next
return data, nil
}
// Peek returns the top item from the queue (without removing it)
func (q *Queue[T]) Peek() (T, error) {
if q.IsEmpty() {
return *new(T), errors.New("empty Queue error")
}
return q.first.data, nil
}
// IsEmpty returns true when the queue is empty, false otherwise
func (q *Queue[T]) IsEmpty() bool {
return q.first == nil
}