본문 바로가기
Algorithm/📖Baekjoon

#10845 큐

by yewoneeee 2022. 4. 24.

# 문제

# 입력 및 출력

# 풀이

전의 스택 문제와 똑같음

그냥 c++에 존재하는 queue헤더를 사용해서 풀었음

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

int main() {
	int n, num;
	cin >> n;
	queue<int> q;
	for (int i = 0; i < n; i++) {
		string cmd;
		cin >> cmd;
		if (cmd == "push") {
			cin >> num;
			q.push(num);
		}
		else if (cmd == "pop") {
			if (q.empty()) cout << -1 << endl;
			else {
				cout << q.front() << endl;
				q.pop();
			}	
		}
		else if (cmd == "size") {
			cout << q.size() << endl;
		}
		else if (cmd == "empty") {
			cout << (int)q.empty() << endl;
		}
		else if (cmd == "front") {
			if (q.empty()) cout << -1 << endl;
			else cout << q.front() << endl;
		}
		else { // back
			if (q.empty()) cout << -1 << endl;
			else cout << q.back() << endl;
		}
	}
}

'Algorithm > 📖Baekjoon' 카테고리의 다른 글

#5622 다이얼  (0) 2022.04.24
#1920 수 찾기  (0) 2022.04.24
#1260 DFS와 BFS  (0) 2022.04.23
#1978 소수 찾기  (0) 2022.04.23
#9012 괄호  (0) 2022.04.22

댓글