-
Notifications
You must be signed in to change notification settings - Fork 277
/
DesignCircularQueue.java
81 lines (73 loc) · 1.7 KB
/
DesignCircularQueue.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
70
71
72
73
74
75
76
77
78
79
80
81
class MyCircularQueue {
int F;
int R;
boolean noElPresent;
int[] cqueue = null;
public MyCircularQueue(int k) {
this.cqueue = new int[k];
this.noElPresent = true;
this.F = 0;
this.R = 0;
}
//O(1)
public boolean enQueue(int value) {
if(this.isFull()){
return false;
} else{
this.noElPresent = false;
cqueue[R] =value;
R = (R+1) % cqueue.length;
return true;
}
}
// O(1)
public boolean deQueue() {
if(this.isEmpty()){
return false;
} else{
F = (F+1) % cqueue.length;
if(R==F){
this.noElPresent = true;
}
return true;
}
}
// O(1)
public int Front() {
if(this.isEmpty()){
return -1;
} else{
return cqueue[F];
}
}
// O(1)
public int Rear() {
if(this.isEmpty()){
return -1;
} else{
if(R == 0){
return cqueue[cqueue.length-1];
} else{
return cqueue[R-1];
}
}
}
// O(1)
public boolean isEmpty() {
return this.noElPresent;
}
// O(1)
public boolean isFull() {
return F==R && !this.isEmpty();// F ==R && !this.noElPresent
}
}
/**
* 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();
*/