본문 바로가기

알고리즘/KOALA 알고리즘 스터디

[BOJ/C++] 28279번 덱 2

https://www.acmicpc.net/problem/28279

#include <iostream>
#include <deque>
using namespace std;

deque<int> dq;
int x;

void d(int c)
{
    if (c == 1)
    {
        cin >> x;
        dq.push_front(x);
    }
    else if (c == 2)
    {
        cin >> x;
        dq.push_back(x);
    }
    else if (c == 3)
    {
        if (dq.empty())
        {
            cout << "-1" << "\n";
        }
        else
        {
            cout << dq.front() << "\n";
            dq.pop_front();
        }
    }
    else if (c == 4)
    {
        if (dq.empty())
        {
            cout << "-1" << "\n";
        }
        else
        {
            cout << dq.back() << "\n";
            dq.pop_back();
        }
    }
    else if (c == 5)
    {
        cout << dq.size() << "\n";
    }
    else if (c == 6)
    {
        cout << dq.empty() << "\n";
    }
    else if (c == 7)
    {
        if (dq.empty())
        {
            cout << "-1" << "\n";
        }
        else
        {
            cout << dq.front() << "\n";
        }
    }
    else if (c == 8)
    {
        if (dq.empty())
        {
            cout << "-1" << "\n";
        }
        else
        {
            cout << dq.back() << "\n";
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    int comment;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> comment;
        d(comment);
    }
}