문제
풀이
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
#include<deque>
using namespace std;
int n;
string command;
deque<int> deq;
//덱이 비어있을 때 pop_front, pop_back, front, back 호출 시 런타임에러 발생
//비어있지 않을 때를 if로 설정하기...
int main() {
cin >> n;
while (n--) {
cin >> command;
if (command == "push_front") {
int x;
cin >> x;
deq.push_front(x);
}
else if (command == "push_back") {
int x;
cin >> x;
deq.push_back(x);
}
else if (command == "pop_front") {
if (!deq.empty()) {
cout << deq.front() << '\n';
deq.pop_front();
}
else {
cout << -1 << '\n';
}
}
else if (command == "pop_back") {
if (!deq.empty()) {
cout << deq.back() << '\n';
deq.pop_back();
}
else {
cout << -1 << '\n';
}
}
else if (command == "size") {
cout<<deq.size() << '\n';
}
else if (command == "empty") {
cout<<deq.empty() << '\n';
}
else if (command == "front") {
if (!deq.empty()) {
cout << deq.front() << '\n';
}
else {
cout << -1 << '\n';
}
}
else if (command == "back") {
if (!deq.empty()) {
cout << deq.back() << '\n';
}
else {
cout << -1 << '\n';
}
}
}
}