문제출처 : https://www.acmicpc.net/problem/10866
code
#include <iostream>
#include <deque>
using namespace std;
int main()
{
int N, push_num;
cin >> N;
deque<int>dq;
string cmd;
for (int i = 0; i < N; i++)
{
cin >> cmd;
if (cmd == "push_front")
{
cin >> push_num;
dq.push_front(push_num);
}
else if (cmd == "push_back")
{
cin >> push_num;
dq.push_back(push_num);
}
else if (cmd == "pop_front")
{
if (dq.empty())
cout << -1 << endl;
else
{
int x = dq.front();
dq.pop_front();
cout << x << endl;
}
}
else if (cmd == "pop_back")
{
if (dq.empty())
cout << -1 << endl;
else
{
int x = dq.back();
dq.pop_back();
cout << x << endl;
}
}
else if (cmd == "size")
cout << dq.size() << endl;
else if (cmd == "empty")
{
if (dq.empty())
cout << 1 << endl;
else
cout << 0 << endl;
}
else if (cmd == "front")
{
if (dq.empty())
cout << -1 << endl;
else
cout << dq.front() << endl;
}
else if (cmd == "back")
{
if (dq.empty())
cout << -1 << endl;
else
cout << dq.back() << endl;
}
}
return 0;
}
단순 데크큐(deque) 구현문제다.