-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10866.cpp
69 lines (60 loc) · 1.79 KB
/
10866.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
#include <iostream>
#include <string>
using namespace std;
int main() {
int N;
cin >> N;
int deque[10000 + 1];
const int capacity = sizeof deque / sizeof deque[0];
int front = 0, rear = 0;
while (N-- > 0) {
string command;
cin >> command;
if (command == "push_front") {
string s;
cin >> s;
deque[front] = stoi(s);
front = (front - 1 + capacity) % capacity;
} else if (command == "push_back") {
string s;
cin >> s;
rear = (rear + 1) % capacity;
deque[rear] = stoi(s);
} else if (command == "pop_front") {
if (front == rear)
cout << -1 << '\n';
else {
front = (front + 1) % capacity;
cout << deque[front] << '\n';
}
} else if (command == "pop_back") {
if (rear == front)
cout << -1 << '\n';
else {
cout << deque[rear] << '\n';
rear = (rear - 1 + capacity) % capacity;
}
} else if (command == "size") {
int count = 0;
int it = front;
while (it != rear) {
++count;
it = (it + 1) % capacity;
}
cout << count << '\n';
} else if (command == "empty") {
cout << (front == rear) << '\n';
} else if (command == "front") {
if (front == rear)
cout << -1 << '\n';
else
cout << deque[(front + 1) % capacity] << '\n';
} else if (command == "back") {
if (front == rear)
cout << -1 << '\n';
else
cout << deque[rear] << '\n';
}
}
return 0;
}