학사 나부랭이

백준 10866 - 덱 본문

塵箱/코드 삽질

백준 10866 - 덱

태양왕 해킹 (14세) 2021. 4. 19. 21:44

덱이란?

 큐와 비슷하지만 앞뒤로 다 넣고 뺄 수 있는 빨대 같은 구조

#include <iostream>
#include <cstring>
using namespace std;
struct deque {
	int* data;
	int blockn, front, back;
public:
	void pushf(int i);
	void pushb(int i);
	int popf();
	int popb();
	int size();
	int empty();
	int frt();
	int bak();
	deque();
	~deque();
};

deque::deque() {
	blockn = 10;
	front = back = 0;
	data = new int[blockn];
}
deque::~deque() { delete[] data; }

void deque::pushf(int i) {
	if (back >= blockn - 1) {
		int* tmparr = new int[blockn + 10];
		for (int i = front; i < back; i++) tmparr[i - front + 1] = data[i];
		delete[] data;
		back -= front;
		front = 0;
		blockn += 10;
		data = tmparr;
		back++;
		tmparr[front] = i;
	}
	else {
		int* tmparr = new int[blockn];
		for (int i = front; i < back; i++) tmparr[i - front + 1] = data[i];
		back -= front;
		front = 0;
		delete[] data;
		data = tmparr;
		back++;
		data[front] = i;
	}
}
void deque::pushb(int i) {
	if (back >= blockn - 1) {
		int* tmparr = new int[blockn + 10];
		for (int i = front; i < back; i++) tmparr[i - front] = data[i];
		delete[] data;
		back -= front;
		front = 0;
		blockn += 10;
		data = tmparr;
		back++;
		tmparr[back - 1] = i;
	}
	else {
		back++;
		data[back - 1] = i;
	}
}
int deque::popf() {
	if (front >= back) return -1;
	front++;
	return data[front - 1];
}
int deque::popb() {
	if (front >= back) return -1;
	back--;
	return data[back];
}
int deque::size() { return back - front; }
int deque::empty() {
	if (front >= back) return 1;
	return 0;
}
int deque::frt() {
	if (front >= back) return -1;
	return data[front];
}
int deque::bak() {
	if (front >= back) return -1;
	return data[back - 1];
}

int main() {
	int cs, idx = 0, * arr, * deq, front = 0, back = 0, n = 10;
	string inst;
	cin >> cs;
	arr = new int[cs];
	deque d;
	//deq = new int[n];
	for (int i = 0; i < cs; i++) {
		cin >> inst;
		if (inst.compare("push_front") == 0) {
			//if (back >= n) {
			//	int* tmparr = new int[n + 10];
			//	for (int j = front; j < back; j++) tmparr[j - front + 1] = deq[j];
			//	back -= front;
			//	front = 0;
			//	delete[] deq;
			//	n += 10;
			//	deq = tmparr;
			//	cin >> deq[front];
			//}
			//else {
			//	for (int j = front; j < back; j++) deq[j + 1] = deq[j];
			//	cin >> deq[front];
			//}
			cin >> arr[i];
			arr[i] += 10;
		}
		else if (inst.compare("push_back") == 0) {
			cin >> arr[i];
			arr[i] *= -1;
		}
		else if (inst.compare("pop_front") == 0) arr[i] = 1;
		else if (inst.compare("pop_back") == 0) arr[i] = 2;
		else if (inst.compare("size") == 0) arr[i] = 3;
		else if (inst.compare("empty") == 0) arr[i] = 4;
		else if (inst.compare("front") == 0) arr[i] = 5;
		else if (inst.compare("back") == 0) arr[i] = 6;
		else arr[i] = -999999;
	}
	for (int i = 0; i < cs; i++) {
		if (arr[i] > 10) d.pushf(arr[i] - 10);
		else if (arr[i] < 0) d.pushb(arr[i] * (-1));
		else if (arr[i] == 1) cout << d.popf() << "\n";
		else if (arr[i] == 2) cout << d.popb() << "\n";
		else if (arr[i] == 3) cout << d.size() << "\n";
		else if (arr[i] == 4) cout << d.empty() << "\n";
		else if (arr[i] == 5) cout << d.frt() << "\n";
		else if (arr[i] == 6) cout << d.bak() << "\n";
		else if (arr[i] < 0) cout << "input error\n";
	}
	delete[] arr;
	//d.~deque();
}

'塵箱 > 코드 삽질' 카테고리의 다른 글

백준 2869 - 조건 적확하게 설정하기  (0) 2021.04.20
백준 15829 - 모듈러 연산  (0) 2021.04.20
백준 1494 - 다이나믹 프로그래밍, 이진 탐색  (0) 2021.04.17
Floyd algorithm  (0) 2021.04.16
백준 2164 - 큐 구현  (0) 2021.04.16
Comments