10866_덱(C++)

1980 단어 덱(Deque)덱(Deque)

#include <iostream>
using namespace std;

struct Deque
{
	int deque[10000];
	int begin = 5000;
	int end = 5001;
	void push_front(int data) {
		deque[begin] = data;
		begin--;
	}
	void push_back(int data) {
		deque[end] = data;
		end++;
	}
	void pop_front() {
		if (!empty()) {
			cout << deque[begin + 1] << '\n';
			begin++;
		}
		else cout << -1 << '\n';
	}
	void pop_back() {
		if (!empty()) {
			cout << deque[end-1] << '\n';
			end--;
		}
		else cout << -1 << '\n';
	}
	void size() {
		cout << end - begin -1 << '\n';
	}
	bool empty() {
		if (begin + 1 == end) return 1;
		else return 0;
	}
	void front() {
		if (!empty()) cout << deque[begin + 1] << '\n';
		else cout << -1 << '\n';
	}
	void back() {
		if (!empty()) cout << deque[end - 1] << '\n';
		else cout << -1 << '\n';
	}
};
int main() {
	int N;
	string command;
	Deque mine;
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> command;
		if (command == "push_front") {
			int k = 0;
			cin >> k;
			mine.push_front(k);
		}
		else if(command == "push_back") {
			int k = 0;
			cin >> k;
			mine.push_back(k);
		}
		else if (command == "pop_front") {
			mine.pop_front();
		}
		else if (command == "pop_back") {
			mine.pop_back();
		}
		else if (command == "size") {
			mine.size();
		}
		else if (command == "empty") {
			cout << mine.empty() << '\n';
		}
		else if (command == "front") {
			mine.front();
		}
		else if (command == "back") {
			mine.back();
		}
	}
}

덱 설명 참고:
https://velog.io/@kimeunseo58/%EB%8D%B1

좋은 웹페이지 즐겨찾기