[백준] 10866 덱

0

백준

목록 보기
188/271
post-thumbnail

[백준] 10866 덱

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

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int n;
	cin >> n;

	deque<int> dq;
	while (n--) {
		string op;
		cin >> op;
		if (op == "push_front") {
			int x;
			cin >> x;
			dq.push_front(x);
		}
		else if (op == "push_back") {
			int x;
			cin >> x;
			dq.push_back(x);
		}
		else if (op == "pop_front") {
			if (dq.empty()) cout << "-1\n";
			else {
				cout << dq.front()<<"\n";
				dq.pop_front();
			}
		}
		else if (op == "pop_back") {
			if (dq.empty()) cout << "-1\n";
			else {
				cout << dq.back() << "\n";
				dq.pop_back();
			}
		}
		else if (op == "size") {
			cout << dq.size() << "\n";
		}
		else if (op == "empty") {
			cout << dq.empty()<<"\n";
		}
		else if (op == "front") {
			if (dq.empty()) cout << "-1\n";
			else cout << dq.front() << "\n";
		}
		else if (op == "back") {
			if (dq.empty()) cout << "-1\n";
			else cout << dq.back() << "\n";
		}
	}

	return 0;
}
profile
Be able to be vulnerable, in search of truth

0개의 댓글