[백준] 10845번 : 큐

2021. 1. 19. 20:14Algorithm/백준

 

10845번: 큐

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

 

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

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n, k;
	string s;
	queue<int>q;

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

		cin >> s;

		if (s == "push") {
			cin >> k;
			q.push(k);
		}

		else if (s == "front") {
			if (q.size() != 0)
				cout << q.front() << '\n';
			else cout << -1 << '\n';
		}

		else if (s == "size") {
			cout << q.size() << '\n';
		}

		else if (s == "pop") {
			if (q.size() != 0) {
				cout << q.front() << '\n';
				q.pop();
			}
			else cout << -1 << '\n';
		}

		else if (s == "empty") {
			if (q.empty())
				cout << 1 << '\n';
			else cout << 0 << '\n';
		}

		else if (s == "back") {
			if (q.empty())
				cout << -1 << '\n';
			else cout << q.back() << '\n';
		}
	}
}

'Algorithm > 백준' 카테고리의 다른 글

[백준] 10808번 : 알파벳 개수  (0) 2021.01.19
[백준] 10866번: 덱  (0) 2021.01.19
[백준] 1260번 : DFS와 BFS  (0) 2021.01.19
[백준] 9012번: 괄호  (0) 2021.01.19
[백준] 10828번: 스택  (0) 2021.01.19