백준 19845번 : 큐 문제와 동일하나 큐가 아닌 덱을 이용한다는 점만 다르다.
#include<iostream>
#include<deque>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
int N;
scanf("%d", &N);
cin.ignore();
deque<int> myDeque;
for (int i = 0; i < N; i++)
{
string params;
getline(cin, params);
int blankIndex = params.find(" ");
string param1 = params.substr(0, blankIndex);
if (param1.compare("push_front") == 0)
{
int number = stoi(params.substr(blankIndex + 1, params.length()));
myDeque.push_front(number);
}
else if (param1.compare("push_back") == 0)
{
int number = stoi(params.substr(blankIndex + 1, params.length()));
myDeque.push_back(number);
}
else if (param1.compare("pop_front") == 0)
{
if (myDeque.empty())
{
printf("-1\n");
}
else
{
int number = myDeque.front();
printf("%d\n", number);
myDeque.pop_front();
}
}
else if (param1.compare("pop_back") == 0)
{
if (myDeque.empty())
{
printf("-1\n");
}
else
{
int number = myDeque.back();
printf("%d\n", number);
myDeque.pop_back();
}
}
else if (param1.compare("size") == 0)
{
printf("%d\n", myDeque.size());
}
else if (param1.compare("empty") == 0)
{
if (myDeque.empty())
{
printf("1\n");
}
else
{
printf("0\n");
}
}
else if (param1.compare("front") == 0)
{
if (myDeque.empty())
{
printf("-1\n");
}
else
{
printf("%d\n", myDeque.front());
}
}
else if (param1.compare("back") == 0)
{
if (myDeque.empty())
{
printf("-1\n");
}
else
{
printf("%d\n", myDeque.back());
}
}
}
return 0;
}
'개발일지 > 알고리즘' 카테고리의 다른 글
백준 11651번 : 좌표 정렬하기 2 (0) | 2025.01.23 |
---|---|
백준 1181번 : 단어 정렬 (0) | 2025.01.22 |
백준 10845번 : 큐 (0) | 2025.01.20 |
백준 10828번 : 스택 (1) | 2025.01.19 |
백준 10815번 : 숫자 카드 (0) | 2025.01.19 |