코딩테스트/백준

[백준] 18258번 큐 2 (C++)

Deff_a 2024. 12. 19. 10:48

문제

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


풀이

#include <iostream>
#include <queue>
#include <string>

using namespace std;

queue<int> q;

int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;

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

		int result = 0;
		if(input == "push")
		{
			cin >> input;
			int pushNum = stoi(input);
			q.push(pushNum);
			continue;
		}

		if(input == "pop" || input == "front")
		{
			result = q.empty() ? -1 : q.front();
			if (!q.empty() && input == "pop")
				q.pop();
		}
		else if (input == "back")
			result = q.empty() ? -1 : q.back();
		else if (input == "empty")
			result = q.empty() ? 1 : 0;
		else if (input == "size")
			result = q.size();

		cout << result << "\n";
	}

	return 0;
}