-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathqueue.go
67 lines (54 loc) · 1.4 KB
/
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
62
63
64
65
66
67
package stl4go
import (
"fmt"
)
// Queue is a FIFO container
type Queue[T any] struct {
list DList[T]
}
// NewQueue create a new Queue object.
func NewQueue[T any]() *Queue[T] {
q := Queue[T]{}
q.list.Clear()
return &q
}
// Len implements the Container interface.
func (q *Queue[T]) Len() int {
return q.list.Len()
}
// IsEmpty implements the Container interface.
func (q *Queue[T]) IsEmpty() bool {
return q.list.IsEmpty()
}
// Clear implements the Container interface.
func (q *Queue[T]) Clear() {
q.list.Clear()
}
// Len implements the fmt.Stringer interface.
func (q *Queue[T]) String() string {
return fmt.Sprintf("Queue[%v]", nameOfType[T]())
}
// PushFront pushed an element to the front of the queue.
func (q *Queue[T]) PushFront(val T) {
q.list.PushFront(val)
}
// PushBack pushed an element to the back of the queue.
func (q *Queue[T]) PushBack(val T) {
q.list.PushBack(val)
}
// PopFront popups an element from the front of the queue.
func (q *Queue[T]) PopFront() T {
return q.list.PopFront()
}
// PopBack popups an element from the back of the queue.
func (q *Queue[T]) PopBack() T {
return q.list.PopBack()
}
// TryPopFront tries popuping an element from the front of the queue.
func (q *Queue[T]) TryPopFront() (T, bool) {
return q.list.TryPopFront()
}
// TryPopBack tries popuping an element from the back of the queue.
func (q *Queue[T]) TryPopBack() (T, bool) {
return q.list.TryPopBack()
}