3 분 소요

🗒️ 10828번) 스택

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

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int N;
	cin >> N;

	stack<int> s;
	while (N--)
	{
		string comm;
		cin >> comm;

		if (comm == "push")
		{
			int num;
			cin >> num;

			s.push(num);
		}
		else if (comm == "pop")
		{
			if (s.size() > 0)
			{
				cout << s.top() << '\n';
				s.pop();
			}
			else
				cout << -1 << '\n';
		}
		else if (comm == "size")
		{
			cout << s.size() << '\n';
		}
		else if (comm == "empty")
		{
			cout << s.empty() << '\n';
		}
		else if (comm == "top")
		{
			if (s.size() > 0)
				cout << s.top() << '\n';
			else
				cout << -1 << '\n';
		}
	}
	
	return 0;
}

카테고리:

업데이트:

댓글남기기