알고리즘

C++ 알고리즘 - 백준 18528 큐 2

마루설아 2025. 1. 12. 20:49

https://www.acmicpc.net/problem/18258

 

#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

void CPP_INIT() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}

int main(void) {
	CPP_INIT();

	int input;
	int num;
	string str;
	queue<int> qu;
	cin >> input;

	for (int i = 0; i < input; i++) {
		cin >> str;

		if (str.find("push") != -1) {
			cin >> num;
			qu.push(num);
		}

		else if (str == "pop") {
			if (qu.empty()) cout << -1 << endl;
			else {
				num = qu.front();
				qu.pop();
				cout << num << endl;
			}
		}

		else if (str == "size") {
			cout << qu.size() << endl;
		}

		else if (str == "empty") {
			cout << qu.empty() << endl;
		}

		else if (str == "front") {
			if (qu.empty()) cout << -1 << endl;
			else cout << qu.front() << endl;
		}

		else if (str == "back") {
			if (qu.empty()) cout << -1 << endl;
			else cout << qu.back() << endl;
		}
	}
}