forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign-circular-queue.go
executable file
·72 lines (61 loc) · 1.59 KB
/
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
72
package problem0622
// MyCircularQueue 结构体
type MyCircularQueue struct {
queue []int
k int
}
// Constructor initialize your data structure here. Set the size of the queue to be k.
func Constructor(k int) MyCircularQueue {
return MyCircularQueue{
queue: make([]int, 0, k*3),
k: k,
}
}
// EnQueue insert an element into the circular queue. Return true if the operation is successful.
func (m *MyCircularQueue) EnQueue(value int) bool {
if len(m.queue) == m.k {
return false
}
m.queue = append(m.queue, value)
return true
}
// DeQueue delete an element from the circular queue. Return true if the operation is successful.
func (m *MyCircularQueue) DeQueue() bool {
if len(m.queue) == 0 {
return false
}
m.queue = m.queue[1:]
return true
}
// Front get the front item from the queue.
func (m *MyCircularQueue) Front() int {
if len(m.queue) == 0 {
return -1
}
return m.queue[0]
}
// Rear get the last item from the queue. */
func (m *MyCircularQueue) Rear() int {
if len(m.queue) == 0 {
return -1
}
return m.queue[len(m.queue)-1]
}
// IsEmpty checks whether the circular queue is empty or not. */
func (m *MyCircularQueue) IsEmpty() bool {
return len(m.queue) == 0
}
// IsFull checks whether the circular queue is full or not. */
func (m *MyCircularQueue) IsFull() bool {
return len(m.queue) == m.k
}
/**
* 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();
*/