4 분 소요

🗒️ 10845번) 큐

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

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int N;
	cin >> N;

	queue<int> q;
	while (N--)
	{
		string cmd;
		cin >> cmd;

		if (cmd == "push")
		{
			int num;
			cin >> num;

			q.push(num);
		}
		else if (cmd == "pop")
		{
			if (!q.empty())
			{
				cout << q.front() << '\n';
				q.pop();
			}
			else
				cout << -1 << '\n';
		}
		else if (cmd == "size")
		{
			cout << q.size() << '\n';
		}
		else if (cmd == "empty")
		{
			cout << q.empty() << '\n';
		}
		else if (cmd == "front")
		{
			if (!q.empty())
				cout << q.front() << '\n';
			else
				cout << -1 << '\n';
		}
		else if (cmd == "back")
		{
			if (!q.empty())
				cout << q.back() << '\n';
			else
				cout << -1 << '\n';
		}
	}

	return 0;
}

카테고리:

업데이트:

댓글남기기