https://www.acmicpc.net/problem/10866
10866: 데크
첫 번째 줄에 지정된 명령의 수 N(1 ≤ N ≤ 10,000)이 주어집니다. 두 번째 줄부터 N개의 줄은 각각 명령을 받습니다. 지정된 정수는 1보다 크거나 같고 100,000보다 작거나 같습니다. 문제에 있다
www.acmicpc.net
문제를 해결하다
deque라는 데이터 구조는 이미 C++에 정의되어 있습니다.
- push_front(): 앞에 추가
- push_back(): 뒤에 추가
- pop_front(): 앞에서 삭제
- pop_back(): 뒤에서 삭제
- empty(), size(): 등은 다른 stl과 동일하게 사용할 수 있습니다.
소스 코드
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <deque>
#include <string>
using namespace std;
deque<int> d;
int n;
void solution() {
scanf("%d", &n);
string order;
int x = 0;
for (int i = 0; i < n; i++) {
cin >> order;
if (order == "push_front") {
scanf("%d", &x);
d.push_front(x);
}
if (order == "push_back") {
scanf("%d", &x);
d.push_back(x);
}
if (order == "pop_front" || order == "front") {
if (d.empty() != true) {
printf("%d\n", d.front());
if(order == "pop_front") d.pop_front();
}
else printf("-1\n");
}
if (order == "pop_back" || order == "back") {
if (d.empty() != true) {
printf("%d\n", d.back());
if(order == "pop_back") d.pop_back();
}
else printf("-1\n");
}
if (order == "size") printf("%d\n", d.size());
if (order == "empty") printf("%d\n", d.empty());
}
}
int main() {
solution();
}