-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularqueue.go
More file actions
53 lines (45 loc) · 847 Bytes
/
Copy pathcircularqueue.go
File metadata and controls
53 lines (45 loc) · 847 Bytes
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
package p622
type MyCircularQueue struct {
items []int
cap, len, front int
}
func NewMyCircularQueue(k int) MyCircularQueue {
return MyCircularQueue{
cap: k,
items: make([]int, k),
}
}
func (q *MyCircularQueue) EnQueue(value int) bool {
if q.len == q.cap {
return false
}
q.items[(q.front+q.len)%q.cap] = value
q.len++
return true
}
func (q *MyCircularQueue) DeQueue() bool {
if q.len == 0 {
return false
}
q.front = (q.front + 1) % q.cap
q.len--
return true
}
func (q *MyCircularQueue) Front() int {
if q.len == 0 {
return -1
}
return q.items[q.front]
}
func (q *MyCircularQueue) Rear() int {
if q.len == 0 {
return -1
}
return q.items[(q.front+q.len-1)%q.cap]
}
func (q *MyCircularQueue) IsEmpty() bool {
return q.len == 0
}
func (q *MyCircularQueue) IsFull() bool {
return q.len == q.cap
}