-
Notifications
You must be signed in to change notification settings - Fork 13
/
MyCircularQueue.java
69 lines (52 loc) · 1.16 KB
/
MyCircularQueue.java
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
package leetcode.queue;
public class MyCircularQueue {
private final int[] nums;
private int left;
private int right;
private int size;
public MyCircularQueue(int k) {
nums = new int[k];
left = 0;
right = 0;
size = 0;
}
public boolean enQueue(int value) {
if (isFull()) {
return false;
} else {
// 进入队列右指针移动
nums[right % nums.length] = value;
right++;
size++;
return true;
}
}
public boolean deQueue() {
if (isEmpty()) {
return false;
} else {
// 出栈左指针移动
left++;
size--;
return true;
}
}
public int Front() {
if (isEmpty()) {
return -1;
}
return nums[left % nums.length];
}
public int Rear() {
if (isEmpty()) {
return -1;
}
return nums[(right - 1) % nums.length];
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == nums.length;
}
}