-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboj10866.cpp
102 lines (90 loc) · 2.14 KB
/
boj10866.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
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
inline void Quick_IO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
class Deque {
private:
int *arr;
int head;
int tail;
int length;
public:
Deque(int n) {
arr = new int[n];
head = 0;
tail = 1;
length = n + 1;
}
int pop_back() {
int ret = back();
tail = (tail - 1 + length) % length;
return ret;
}
int pop_front() {
int ret = front();
head = (head + 1) % length;
return ret;
}
void push_back(int x) {
arr[tail] = x;
tail = (tail + 1) % length;
}
void push_front(int x) {
arr[head] = x;
head = (head - 1 + length) % length;
}
bool empty() {
return !size();
}
int back() {
return arr[(tail - 1 + length) % length];
}
int front() {
return arr[(head + 1) % length];
}
int size() {
return (tail - head + length) % length - 1;
}
};
int main() {
Quick_IO();
int Q, num;
cin >> Q;
string q;
Deque d(Q);
for (int i = 0; i < Q; ++i) {
cin >> q;
if (cin.peek() == ' ') {
cin >> num;
if (q == "push_back") {
d.push_back(num);
} else {
d.push_front(num);
}
} else {
if (q == "pop_front") {
if (d.empty()) cout << "-1\n";
else cout << d.pop_front() << '\n';
} else if (q == "pop_back") {
if (d.empty()) cout << "-1\n";
else cout << d.pop_back() << '\n';
} else if (q == "size") {
cout << d.size() << '\n';
} else if (q == "empty") {
cout << d.empty() << '\n';
} else if (q == "front") {
if (d.empty()) cout << "-1\n";
else cout << d.front() << '\n';
} else if (q == "back") {
if (d.empty()) cout << "-1\n";
else cout << d.back() << '\n';
}
}
}
return 0;
}