(cpp) Baekjoon 10845번 문제 ‘큐’ - 자료구조

Baekjoon 10845번 문제 ‘큐’ - 자료구조


문제

풀이

#define _CRT_SECURE_NO_WARNING
#include <iostream>
#include <string>
#include<queue>

using namespace std;
int n;
int x;
string command;
queue <int> que;

int main() {
	cin >> n;
	while (n--) {
		cin >> command;
		if (command == "push") {
			cin >> x;
			que.push(x);
		}

		else if (command == "pop") {
			if (!que.empty()) {
				cout << que.front() << '\n';
				que.pop();
			}
			else {
				cout << -1 << '\n';
			}
		}

		else if (command == "size") {
			cout << que.size() << '\n';
		}

		else if (command == "empty") {
			cout << que.empty() << '\n';
		}

		else if (command == "front") {
			if (!que.empty()) {
				cout << que.front() << '\n';
			}
			else {
				cout << -1 << '\n';
			}
		}

		else if (command == "back") {
			if (!que.empty()) {
				cout << que.back() << '\n';
			}
			else {
				cout << -1 << '\n';
			}
		}
	}
}