-
Notifications
You must be signed in to change notification settings - Fork 26
/
622. Design Circular Queue
66 lines (56 loc) · 1.3 KB
/
622. Design Circular Queue
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
class MyCircularQueue {
int[] q;
int front=0,rear=0,size=0;
public MyCircularQueue(int k) {
q=new int[k];
Arrays.fill(q,-1);
}
public boolean enQueue(int value) {
if(isFull()){
return false;
}
if(isEmpty()){
front=rear=0;
q[rear]=value;
size++;
return true;
}
rear++;
size++;
rear = rear %q.length;
q[rear]=value;
return true;
}
public boolean deQueue() {
if(isEmpty()){
return false;
}
q[front]=-1;
size--;
front++;
front = front % q.length;
return true;
}
public int Front() {
return q[front];
}
public int Rear() {
return q[rear];
}
public boolean isEmpty() {
return size==0;
}
public boolean isFull() {
return size==q.length;
}
}
/**
* 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();
*/