-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesignCircularQueue.java
More file actions
47 lines (38 loc) · 899 Bytes
/
designCircularQueue.java
File metadata and controls
47 lines (38 loc) · 899 Bytes
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
class MyCircularQueue {
int[] q;
int front, rear, size, capacity;
public MyCircularQueue(int k) {
capacity = k;
q = new int[k];
front = 0;
rear = 0;
size = 0;
}
public boolean enQueue(int value) {
if (isFull()) return false;
q[rear] = value;
rear = (rear + 1) % capacity;
size++;
return true;
}
public boolean deQueue() {
if (isEmpty()) return false;
front = (front + 1) % capacity;
size--;
return true;
}
public int Front() {
if (isEmpty()) return -1;
return q[front];
}
public int Rear() {
if (isEmpty()) return -1;
return q[(rear - 1 + capacity) % capacity];
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == capacity;
}
}