코딩테스트/백준

[실4] 28278 - 스택 2

ShovelingLife 2023. 8. 29. 08:46
#include <iostream>
#include <string>
#include <stack>

using namespace std;

stack<int> s;
string res;

void Push(int n) { s.push(n); }

void Pop()
{
	if (s.empty())
		res += to_string(-1) + '\n';

	else
	{
		res += to_string(s.top()) + '\n';
		s.pop();
	}
}

void Size() { res += to_string(s.size()) + '\n'; }

void Empty() { res += to_string(s.empty()) + '\n'; }

void Top() { res += to_string(s.empty() ? -1 : s.top()) + '\n'; }

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

	int n; cin >> n;

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

		switch (opt)
		{
		case 1:
			int tmp; cin >> tmp;
			Push(tmp);
			break;

		case 2: Pop(); break;

		case 3: Size(); break;

		case 4: Empty(); break;

		case 5: Top(); break;
		}
	}
	res.pop_back();
	cout << res;
	return 0;
}