-
Notifications
You must be signed in to change notification settings - Fork 0
/
247.c
51 lines (44 loc) · 1.07 KB
/
247.c
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
// http://www.bjfuacm.com/problem/247/
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int* base;
int front, rear;
int tag;
} Queue;
void Create(Queue* Q, int len) {
Q->base = calloc(len, sizeof(int));
if (!Q->base) exit(12);
(*Q).front = (*Q).rear = 0;
(*Q).tag = 0;
}
void enqueue(Queue* Q, int e, int n) {
if ((Q->tag == 1) && (Q->rear == Q->front)) exit(12);
Q->base[Q->rear] = e;
Q->rear = (Q->rear + 1) % n;
if (Q->tag == 0) Q->tag = 1;
}
int dequeue(Queue* Q, int n) {
if ((Q->tag == 0) && (Q->rear == Q->front)) exit(13);
int ans = Q->base[Q->front];
Q->front = (Q->front + 1) % n;
if (Q->tag == 1) Q->tag = 0;
return ans;
}
int main() {
int n;
while (1) {
scanf("%d", &n);
if (n == 0) break;
Queue Q;
Create(&Q, n);
int a;
for (int i = 0; i < n; i++) {
scanf("%d", &a);
enqueue(&Q, a, n);
}
for (int i = 0; i < n; i++) {
printf("%d%s", dequeue(&Q, n), i == n - 1 ? "\n" : " ");
}
}
}