10866インデックス


#include <iostream> 
#include <vector>
#include<sstream>
#include <algorithm>
#include <string>
#include <utility>
#include <deque>

using namespace std;

int main() {
	deque<int> s;
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		string command;
		int num;
		cin >> command;
		if (command == "push_front") {
			cin >> num;
			s.push_front(num);

		}

		else if (command == "push_back") {
			cin >> num;
			s.push_back(num);

		}

		else if (command == "pop_front") {
			if (s.empty()) {
				cout << -1 << endl;

			}
			else {
				cout << s.front() << endl;
				s.pop_front();
			}

		}

		else if (command == "pop_back") {
			if (s.empty()) {
				cout << -1 << endl;

			}
			else {
				cout << s.back() << endl;
				s.pop_back();
			}

		}

		else if (command == "size") {
			cout << s.size() << endl;

		}
		else if (command == "empty") {
			if (s.empty()) {
				cout << 1 << endl;

			}
			else {
				cout << 0 << endl;

			}
		}
		else if (command == "front") {
			if (s.empty()) {
				cout << -1 << endl;

			}
			else {
				cout << s.front() << endl;

			}
		}
		else if (command == "back") {
			if (s.empty()) {
				cout << -1 << endl;

			}
			else {
				cout << s.back() << endl;

			}
		}
	}

}