(cpp) Baekjoon 10828번 문제 ‘스택’ - 자료구조

Baekjoon 10828번 문제 ‘스택’ - 자료구조


문제

풀이

#define _CRT_SECURE_NO_WARNING
#include <iostream>
#include <string>
#include<stack>

using namespace std;
int n;
int x;
string command;
stack <int> st;

int main() {
	cin >> n;
	while (n--) {
		cin >> command;
		if (command == "push") {
			cin >> x;
			st.push(x);
		}

		else if (command == "pop") {
			if (!st.empty()) {
				cout << st.top() << '\n';
				st.pop();
			}
			else {
				cout << -1 << '\n';
			}
		}

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

		else if (command == "empty") {
			cout << st.empty() << '\n';
		}

		else if (command == "top") {
			if (!st.empty()) {
				cout << st.top() << '\n';
			}
			else {
				cout << -1 << '\n';
			}
		}
	}
}