-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeque.go
62 lines (49 loc) · 1.29 KB
/
deque.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
package queue
import "fmt"
// Deque is a double ended queue that supports adding and removing elements
// from both ends of the queue.
type Deque[T any] struct {
list []T
}
// NewDeque returns a new queue.
func NewDeque[T any]() *Deque[T] {
return &Deque[T]{
list: make([]T, 0),
}
}
// IsEmpty returns true if the queue is empty.
func (dq *Deque[T]) IsEmpty() bool {
return len(dq.list) == 0
}
// Size returns the number of nodes in the queue.
func (dq *Deque[T]) Size() int {
return len(dq.list)
}
// PushFront adds a new node to the front of the queue.
func (dq *Deque[T]) PushFront(value T) {
dq.list = append([]T{value}, dq.list...)
}
// PushRear adds a new node to the rear of the queue.
func (dq *Deque[T]) PushRear(value T) {
dq.list = append(dq.list, value)
}
// PopFront removes the first node from the queue.
func (dq *Deque[T]) PopFront() {
dq.list = dq.list[1:]
}
// PopRear removes the last node from the queue.
func (dq *Deque[T]) PopRear() {
dq.list = dq.list[:len(dq.list)-1]
}
// String returns a string representation of the queue.
func (dq *Deque[T]) String() string {
if dq.IsEmpty() {
return "[]"
}
var s string
for i := 0; i < len(dq.list) - 1; i++ {
s += fmt.Sprintf("%v ", dq.list[i])
}
s += fmt.Sprintf("%v", dq.list[len(dq.list) - 1])
return "[" + s + "]"
}