-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18258.cpp
More file actions
82 lines (77 loc) · 1.26 KB
/
18258.cpp
File metadata and controls
82 lines (77 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
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#pragma warning(disable:4996)
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
int queue[2000001];
int front = 0;
int rear = -1;
int size_() {
return rear - front + 1;
}
void front_() {
if (size_() == 0) {
printf("-1\n");
}
else {
printf("%d\n", queue[front]);
}
}
void back_() {
if (size_() == 0) {
printf("-1\n");
}
else {
printf("%d\n", queue[rear]);
}
}
void pop_() {
if (size_() == 0) {
printf("-1\n");
}
else {
printf("%d\n", queue[front++]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
char cmd[6];
cin >> cmd;
if (!strcmp(cmd, "push")) {
int x;
cin >> x;
queue[++rear] = x;
}
else if (!strcmp(cmd, "size")) {
printf("%d\n", size_());
}
else if (!strcmp(cmd, "pop")) {
pop_();
}
else if (!strcmp(cmd, "empty")) {
if (size_() == 0) {
printf("1\n");
}
else {
printf("0\n");
}
}
else if (!strcmp(cmd, "back")) {
back_();
}
else if (!strcmp(cmd, "front")) {
front_();
}
}
return 0;
}