Algorithm
BOJ 10845 큐 C++
최강훈
2020. 7. 6. 14:07
풀이코드
#include <bits/stdc++.h>
using namespace std;
/*
push X: 정수 X를 큐에 넣는 연산이다.
pop: 큐에서 가장 앞에 있는 정수를 빼고, 그 수를 출력한다.
만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
size: 큐에 들어있는 정수의 개수를 출력한다.
empty: 큐가 비어있으면 1, 아니면 0을 출력한다.
front: 큐의 가장 앞에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
back: 큐의 가장 뒤에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
*/
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
queue<int> Q;
int num;
int input;
string s;
cin >> num;
while (num--) {
cin >> s;
if (s == "push") {
cin >> input;
Q.push(input);
} else if (s == "pop") {
if (Q.empty()) {
cout << -1 << '\n';
} else {
cout << Q.front() << '\n';
Q.pop();
}
} else if (s == "size") {
cout << Q.size() << '\n';
} else if (s == "empty") {
cout << Q.empty() << '\n';
} else if (s == "front") {
if (Q.empty())
cout << -1 << '\n';
else
cout << Q.front() << '\n';
} else { //back
if (Q.empty())
cout << -1 << '\n';
else
cout << Q.back() << '\n';
}
}
}