알고리즘

C++ 알고리즘 - 백준 10845 큐

마루설아 2024. 12. 31. 20:52

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

 

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

using namespace std;

int main(void) {
	// C++ Init
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int input;
	int num;
	string str;
	queue<int> qu;
	cin >> input;
	cin.ignore();

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

		if (str.find("push") != -1) {
			string number = str.erase(0, 5);
			num = stoi(number);
			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;
		}
	}
}