-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_queue.go
56 lines (44 loc) · 1.06 KB
/
array_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
package queue
import (
"strings"
arr "go-algorithms/data_structures/array"
)
// ArrayQueue 使用动态数组实现队列
//
// 使用数组头作为队首,数组尾作为队尾
// 示意图为 Front [1, 2, 3, ..., 9] Rear
type ArrayQueue struct {
items *arr.DynamicArray
}
// NewArrayQueue 返回一个空的动态数组实现的队列
func NewArrayQueue() *ArrayQueue {
return &ArrayQueue{arr.New()}
}
func (q *ArrayQueue) Enqueue(data interface{}) {
q.items.Append(data)
}
func (q *ArrayQueue) Dequeue() interface{} {
if q.IsEmpty() {
panic("Can't dequeue from empty queue.")
}
return q.items.Remove(0)
}
func (q *ArrayQueue) GetFront() interface{} {
if q.IsEmpty() {
panic("Can't get front from empty queue.")
}
return q.items.Get(0)
}
func (q *ArrayQueue) IsEmpty() bool {
return q.items.IsEmpty()
}
func (q *ArrayQueue) Size() int {
return q.items.Size()
}
func (q *ArrayQueue) String() string {
var builder strings.Builder
builder.WriteString("Front: ")
builder.WriteString(q.items.String())
builder.WriteString(" Rear")
return builder.String()
}