-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk_queue_impl.cpp
90 lines (81 loc) · 1.47 KB
/
k_queue_impl.cpp
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
82
83
84
85
86
87
88
89
#include <cstdio>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <bitset>
#include <list>
#include <set>
#include <map>
using namespace std;
// same concept as in stack, one FRE pointer point to current free slot.
// front[i] store front of the i th queue.
// rear[i] stores rear of the i th queue.
const int N = 100010;
int n = 100, fre = 0;
int que[N], nxt[N];
int frnt[N], rear[N];
inline void push(int qn, int val) {
int i = fre;
fre = nxt[fre];
if (frnt[qn] == -1) {
frnt[qn] = i;
} else {
nxt[rear[qn]] = i;
}
nxt[i] = -1;
rear[qn] = i;
que[i] = val;
}
inline void pop(int qn) {
if (frnt[qn] != -1) {
int i = frnt[qn];
frnt[qn] = nxt[i];
nxt[i] = fre;
fre = i;
}
}
inline int getFront(int qn) {
if (frnt[qn] == -1) {
return -1;
}
return que[frnt[qn]];
}
inline void init(int k) {
for (int i = 1; i <= k; i++) {
frnt[i] = -1;
}
for (int i = 0; i <= n; i++) {
nxt[i] = i + 1;
}
nxt[n] = -1;
fre = 0;
}
int main() {
int k;
scanf("%d", &k);
init(k);
int q;
scanf("%d", &q);
while (q--) {
int cmd;
scanf("%d", &cmd);
if (cmd == 0) {
int qn, val;
scanf("%d %d", &qn, &val);
push(qn, val);
} else if (cmd == 1) {
int qn;
scanf("%d", &qn);
pop(qn);
} else {
int qn;
scanf("%d", &qn);
printf("%d\n", getFront(qn));
}
}
}