-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
622. Design Circular Queue.go
71 lines (61 loc) · 1.27 KB
/
622. Design Circular 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
68
69
70
71
package leetcode
type MyCircularQueue struct {
cap int
size int
queue []int
left int
right int
}
func Constructor(k int) MyCircularQueue {
return MyCircularQueue{cap: k, size: 0, left: 0, right: 0, queue: make([]int, k)}
}
func (this *MyCircularQueue) EnQueue(value int) bool {
if this.size == this.cap {
return false
}
this.size++
this.queue[this.right] = value
this.right++
this.right %= this.cap
return true
}
func (this *MyCircularQueue) DeQueue() bool {
if this.size == 0 {
return false
}
this.size--
this.left++
this.left %= this.cap
return true
}
func (this *MyCircularQueue) Front() int {
if this.size == 0 {
return -1
}
return this.queue[this.left]
}
func (this *MyCircularQueue) Rear() int {
if this.size == 0 {
return -1
}
if this.right == 0 {
return this.queue[this.cap-1]
}
return this.queue[this.right-1]
}
func (this *MyCircularQueue) IsEmpty() bool {
return this.size == 0
}
func (this *MyCircularQueue) IsFull() bool {
return this.size == this.cap
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* obj := Constructor(k);
* param_1 := obj.EnQueue(value);
* param_2 := obj.DeQueue();
* param_3 := obj.Front();
* param_4 := obj.Rear();
* param_5 := obj.IsEmpty();
* param_6 := obj.IsFull();
*/