백준 10828번 : 스택 문제와 동일하나 스택이 아닌 큐를 이용한다는 점만 다르다.
#include<iostream>
#include<queue>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
int N;
scanf("%d", &N);
cin.ignore();
queue<int> myQueue;
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") == 0)
{
int number = stoi(params.substr(blankIndex + 1, params.length()));
myQueue.push(number);
}
else if (param1.compare("pop") == 0)
{
if (myQueue.empty())
{
printf("-1\n");
}
else
{
int number = myQueue.front();
printf("%d\n", number);
myQueue.pop();
}
}
else if (param1.compare("size") == 0)
{
printf("%d\n", myQueue.size());
}
else if (param1.compare("empty") == 0)
{
if (myQueue.empty())
{
printf("1\n");
}
else
{
printf("0\n");
}
}
else if (param1.compare("front") == 0)
{
if (myQueue.empty())
{
printf("-1\n");
}
else
{
printf("%d\n", myQueue.front());
}
}
else if (param1.compare("back") == 0)
{
if (myQueue.empty())
{
printf("-1\n");
}
else
{
printf("%d\n", myQueue.back());
}
}
}
return 0;
}
'개발일지 > 알고리즘' 카테고리의 다른 글
백준 1181번 : 단어 정렬 (0) | 2025.01.22 |
---|---|
백준 10989번 : 덱 (0) | 2025.01.20 |
백준 10828번 : 스택 (1) | 2025.01.19 |
백준 10815번 : 숫자 카드 (0) | 2025.01.19 |
백준 10989번 : 수 정렬하기 3 (0) | 2025.01.18 |