-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathdesign_circular_queue.go
More file actions
62 lines (51 loc) · 1 KB
/
design_circular_queue.go
File metadata and controls
62 lines (51 loc) · 1 KB
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 main
// 设计循环队列
type MyCircularQueue struct {
data []int
head, tail, size int
}
func Constructor(k int) MyCircularQueue {
return MyCircularQueue{
make([]int, k),
0, 0, 0,
}
}
func (this *MyCircularQueue) EnQueue(value int) bool {
if this.size == len(this.data) {
return false
}
this.data[this.tail] = value
this.tail = (this.tail + 1) % len(this.data)
this.size++
return true
}
func (this *MyCircularQueue) DeQueue() bool {
if this.size == 0 {
return false
}
this.head = (this.head + 1) % len(this.data)
this.size--
return true
}
func (this *MyCircularQueue) Front() int {
if this.size == 0 {
return -1
}
return this.data[this.head]
}
func (this *MyCircularQueue) Rear() int {
if this.size == 0 {
return -1
}
idx := this.tail - 1
if idx < 0 {
idx = len(this.data) - 1
}
return this.data[idx]
}
func (this *MyCircularQueue) IsEmpty() bool {
return this.size == 0
}
func (this *MyCircularQueue) IsFull() bool {
return this.size == len(this.data)
}