본문 바로가기

백준/C++

[C++] 백준 18258번 - 큐 2

728x90

 

문제 18258번

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

 

이번에는 큐이다. 입력값에 따라 조건문을 이용하여 문제에 나와있는 것처럼 수행해주도록 만든다.

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

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	queue <int> q;
	int n, x;
	cin >> n;

	string command;
	while (n--) {
		cin >> command;
		if (command == "push") {
			cin >> x;
			q.push(x);
		}
		else if (command == "pop") {
			if (q.empty()) {
				cout << -1 << '\n';
			}
			else {
				cout << q.front() << '\n';
				q.pop();
			}
		}
		else if (command == "size") {
			cout << q.size() << '\n';
		}
		else if (command == "empty") {
			if (q.empty()) {
				cout << 1 << '\n';
			}
			else {
				cout << 0 << '\n';
			}
		}
		else if (command == "front") {
			if (q.empty()) {
				cout << -1 << '\n';
			}
			else {
				cout << q.front() << '\n';
			}
		}
		else if (command == "back") {
			if (q.empty()) {
				cout << -1 << '\n';
			}
			else {
				cout << q.back() << '\n';
			}
		}
	}

	return 0;
}

 

 

728x90