-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_with_array.c
More file actions
47 lines (47 loc) · 918 Bytes
/
queue_with_array.c
File metadata and controls
47 lines (47 loc) · 918 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
#include <stdio.h>
#include <string.h>
#pragma warning (disable:4996)
int queue[10000];
int main() {
int n, num, top = 0, bot = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
char cmd[BUFSIZ];
scanf("%s", cmd);
if (strcmp(cmd, "push") == 0) {
scanf("%d", &num);
queue[bot] = num;
bot++;
}
if (strcmp(cmd, "pop") == 0) {
if (top == bot)
printf("-1\n");
else {
printf("%d\n", queue[top]);
top++;
}
}
if (strcmp(cmd, "size") == 0) {
printf("%d\n", bot - top);
}
if (strcmp(cmd, "empty") == 0) {
if (top == bot)
printf("1\n");
else
printf("0\n");
}
if (strcmp(cmd, "front") == 0) {
if (top == bot)
printf("-1\n");
else
printf("%d\n", queue[top]);
}
if (strcmp(cmd, "back") == 0) {
if (top == bot)
printf("-1\n");
else
printf("%d\n", queue[bot - 1]);
}
}
return 0;
}