10845_큐

STL : 표준 템플릿 라이브러리
<queue> 사용

#include <iostream> 
#include <vector>
#include<sstream>
#include <algorithm>
#include <string>
#include <utility>
#include <queue>

using namespace std;

int main() {
	queue<int> s;
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		string command;
		int num;
		cin >> command;
		if (command == "push") {
			cin >> num;
			s.push(num);

		}
		else if (command == "pop") {
			if (s.empty()) {
				cout << -1 << endl;

			}
			else {
				cout << s.front() << endl;
				s.pop();
			}

		}
		else if (command == "size") {
			cout << s.size() << endl;

		}
		else if (command == "empty") {
			if (s.empty()) {
				cout << 1 << endl;

			}
			else {
				cout << 0 << endl;

			}
		}
		else if (command == "front") {
			if (s.empty()) {
				cout << -1 << endl;

			}
			else {
				cout << s.front() << endl;

			}
		}
		else if (command == "back") {
			if (s.empty()) {
				cout << -1 << endl;

			}
			else {
				cout << s.back() << endl;

			}
		}
	}

}

좋은 웹페이지 즐겨찾기