본문 바로가기

Coding/백준 온라인 저지 (Baekjoon Online Judge)

백준 온라인 저지(Baekjoon Online Judge) - 10866 : 덱

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

 

10866번: 덱

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

 

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

class Deque {
	int arr[10000];
	int count;
public:
	Deque();
	void push_front(int x);
	void push_back(int x);
	void pop_front();
	void pop_back();
	void size();
	void empty();
	void front();
	void back();
};

Deque::Deque() {
	for (int i = 0; i < 10000; i++) {
		arr[i] = -1;
	}
	count = 0;
}

void Deque::push_front(int x) {
	if (count == 0) {
		arr[count] = x;
	}
	else {
		for (int i = count; i >= 1; i--) {
			arr[i] = arr[i - 1];
		}
		arr[0] = x;
	}
	count++;
}

void Deque::push_back(int x) {
	arr[count] = x;
	count++;
}

void Deque::pop_front() {
	if (count == 0) {
		cout << -1 << endl;
		return;
	}

	int front = arr[0];

	count--;

	for (int i = 0; i < count; i++)
		arr[i] = arr[i + 1];

	arr[count] = -1;

	cout << front << endl;
}

void Deque::pop_back() {
	if (count == 0) {
		cout << -1 << endl;
		return;
	}

	int back = arr[count - 1];
	arr[count - 1] = -1;
	count--;
	
	cout << back << endl;
}

void Deque::size() {
	cout << count << endl;
}

void Deque::empty() {
	if (count == 0) cout << 1 << endl;
	else cout << 0 << endl;
}

void Deque::front() {
	if (count != 0)
		cout << arr[0] << endl;
	else
		cout << -1 << endl;
}

void Deque::back() {
	if (count != 0)
		cout << arr[count - 1] << endl;
	else
		cout << -1 << endl;
}

int main() {
	int n;
	cin >> n;
	string str;
	Deque deque;
	for (int i = 0; i < n; i++) {
		cin >> str;
		if (str == "push_front") {
			int num;
			cin >> num;
			deque.push_front(num);
		}
		else if (str == "push_back") {
			int num;
			cin >> num;
			deque.push_back(num);
		}
		else if (str == "pop_front")
			deque.pop_front();
		else if (str == "pop_back")
			deque.pop_back();
		else if (str == "size")
			deque.size();
		else if (str == "empty")
			deque.empty();
		else if (str == "front")
			deque.front();
		else if (str == "back")
			deque.back();
	}
}