[BOJ10845] 큐 C++

문지영·2023년 5월 7일
0

CODINGTEST C++

목록 보기
2/18

문제

코드

#include <iostream>
#include <string>
#include <queue> // queue<>
using namespace std;

queue<int> q;

int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	while (n--) {
		string oper;
		cin >> oper;

		if (oper == "size") {
			cout << q.size() << '\n';
		}
		else if (oper == "empty") {
			cout << (int) q.empty() << '\n';
		}
		else if (oper == "front") {
			int front = -1;
			if (!q.empty()) front = q.front();
			cout << front << '\n';
		}
		else if (oper == "back") {
			int back = -1;
			if (!q.empty()) back = q.back();
			cout << back << '\n';
		}
		else if (oper == "push") {
			int k;
			cin >> k;
			q.push(k);
		}
		else  {//pop
			int front = -1;
			if (!q.empty()) {
				front = q.front();
				q.pop();
			}
			cout << front << '\n';
		}
	}
}

제출

STL queue 참고

profile
BeHappy

0개의 댓글