-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignCircularQueue.java
More file actions
58 lines (50 loc) · 1.23 KB
/
Copy pathDesignCircularQueue.java
File metadata and controls
58 lines (50 loc) · 1.23 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
class MyCircularQueue {
private final int[] a;
private int start;
private int end;
private int cnt;
private int size;
public MyCircularQueue(int k) {
a = new int[k];
size = k;
}
public boolean enQueue(int value) {
if(cnt == size) return false;
a[end++] = value;
cnt++;
end = end % size;
return true;
}
public boolean deQueue() {
if(cnt == 0) return false;
cnt--;
start++;
start = start % size;
return true;
}
public int Front() {
if(cnt == 0) return -1;
return a[start];
}
public int Rear() {
if(cnt == 0) return -1;
if(end == 0) return a[size-1];
return a[end-1];
}
public boolean isEmpty() {
return cnt == 0;
}
public boolean isFull() {
return cnt == size;
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue obj = new MyCircularQueue(k);
* boolean param_1 = obj.enQueue(value);
* boolean param_2 = obj.deQueue();
* int param_3 = obj.Front();
* int param_4 = obj.Rear();
* boolean param_5 = obj.isEmpty();
* boolean param_6 = obj.isFull();
*/