728x90
큐 문제입니다.
다음을 실행했을 때에 대한 결과값을 출력합니다.
push
pop
size
empty
front
back
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
queue<int> q;
void push(int x)
{
q.push(x);
}
int pop()
{
if (q.empty()) {
return -1;
}
int x = q.front();
q.pop();
return x;
}
int size()
{
return q.size();
}
int empty()
{
if (q.empty()) {
return 1;
}
else {
return 0;
}
}
int front()
{
if (q.empty()) {
return -1;
}
else {
return q.front();
}
}
int back()
{
if (q.empty()) {
return -1;
}
else {
return q.back();
}
}
int main(int argc, char *argv[])
{
int n;
int num;
string s;
cin >> n;
while (n--) {
cin >> s;
if (s == "push") {
cin >> num;
push(num);
}
else if (s == "pop") {
cout << pop() << endl;
}
else if (s == "size") {
cout << size() << endl;
}
else if (s == "empty") {
cout << empty() << endl;
}
else if (s == "front") {
cout << front() << endl;
}
else if (s == "back") {
cout << back() << endl;
}
}
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 1966번 프린터 큐 (0) | 2019.09.08 |
---|---|
[C++] 백준 11866번 조세퍼스 문제 0 (0) | 2019.09.08 |
[C++] 백준 17298번 오큰수 (2) | 2019.09.05 |
[C++] 백준 1874번 스택 수열 (0) | 2019.09.05 |
[C++] 백준 4949번 균형잡힌 세상 (0) | 2019.09.04 |