-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10866.cpp
More file actions
129 lines (111 loc) · 1.83 KB
/
10866.cpp
File metadata and controls
129 lines (111 loc) · 1.83 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <string>
using namespace std;
class Deque {
int arr[10000];
int count;
public:
Deque();
void push_front(int x);
void push_back(int x);
void pop_front();
void pop_back();
void size();
void empty();
void front();
void back();
};
Deque::Deque() {
for (int i = 0; i < 10000; i++) {
arr[i] = -1;
}
count = 0;
}
void Deque::push_front(int x) {
if (count == 0) {
arr[count] = x;
}
else {
for (int i = count; i >= 1; i--) {
arr[i] = arr[i - 1];
}
arr[0] = x;
}
count++;
}
void Deque::push_back(int x) {
arr[count] = x;
count++;
}
void Deque::pop_front() {
if (count == 0) {
cout << -1 << endl;
return;
}
int front = arr[0];
count--;
for (int i = 0; i < count; i++)
arr[i] = arr[i + 1];
arr[count] = -1;
cout << front << endl;
}
void Deque::pop_back() {
if (count == 0) {
cout << -1 << endl;
return;
}
int back = arr[count - 1];
arr[count - 1] = -1;
count--;
cout << back << endl;
}
void Deque::size() {
cout << count << endl;
}
void Deque::empty() {
if (count == 0) cout << 1 << endl;
else cout << 0 << endl;
}
void Deque::front() {
if (count != 0)
cout << arr[0] << endl;
else
cout << -1 << endl;
}
void Deque::back() {
if (count != 0)
cout << arr[count - 1] << endl;
else
cout << -1 << endl;
}
int main() {
int n;
cin >> n;
string str;
Deque deque;
for (int i = 0; i < n; i++) {
cin >> str;
if (str == "push_front") {
int num;
cin >> num;
deque.push_front(num);
}
else if (str == "push_back") {
int num;
cin >> num;
deque.push_back(num);
}
else if (str == "pop_front")
deque.pop_front();
else if (str == "pop_back")
deque.pop_back();
else if (str == "size")
deque.size();
else if (str == "empty")
deque.empty();
else if (str == "front")
deque.front();
else if (str == "back")
deque.back();
}
}