5 분 소요

🗒️ 10866번) 덱

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

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int N;
	cin >> N;

	deque<int> de;
	while (N--)
	{
		string cmd;
		cin >> cmd;

		if (cmd == "push_front")
		{
			int x;
			cin >> x;
			de.push_front(x);
		}
		else if (cmd == "push_back")
		{
			int x;
			cin >> x;
			de.push_back(x);
		}
		else if (cmd == "pop_front")
		{
			if (de.empty())
				cout << -1 << '\n';
			else
			{
				cout << de.front() << '\n';
				de.pop_front();
			}
		}
		else if (cmd == "pop_back")
		{
			if (de.empty())
				cout << -1 << '\n';
			else
			{
				cout << de.back() << '\n';
				de.pop_back();
			}
		}
		else if (cmd == "size")
		{
			cout << de.size() << '\n';
		}
		else if (cmd == "empty")
		{
			cout << de.empty() << '\n';
		}
		else if (cmd == "front")
		{
			if (de.empty())
				cout << -1 << '\n';
			else
				cout << de.front() << '\n';
		}
		else if (cmd == "back")
		{
			if (de.empty())
				cout << -1 << '\n';
			else
				cout << de.back() << '\n';
		}
	}

	return 0;
}

카테고리:

업데이트:

댓글남기기