-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 127.java
More file actions
74 lines (55 loc) · 1.26 KB
/
Day 127.java
File metadata and controls
74 lines (55 loc) · 1.26 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.Arrays;
class kQueues {
private int[] arr;
private int[] next;
private int[] front;
private int[] rear;
private int free;
private int n, k;
kQueues(int n, int k) {
this.n = n;
this.k = k;
arr = new int[n];
next = new int[n];
front = new int[k];
rear = new int[k];
Arrays.fill(front, -1);
Arrays.fill(rear, -1);
for (int i = 0; i < n - 1; i++) {
next[i] = i + 1;
}
next[n - 1] = -1;
free = 0;
}
void enqueue(int x, int i) {
if (isFull()) return;
int idx = free;
free = next[idx];
if (front[i] == -1) {
front[i] = idx;
} else {
next[rear[i]] = idx;
}
next[idx] = -1;
rear[i] = idx;
arr[idx] = x;
}
int dequeue(int i) {
if (isEmpty(i)) return -1;
int idx = front[i];
int result = arr[idx];
front[i] = next[idx];
if (front[i] == -1) {
rear[i] = -1;
}
next[idx] = free;
free = idx;
return result;
}
boolean isEmpty(int i) {
return front[i] == -1;
}
boolean isFull() {
return free == -1;
}
}