Written by JS970
on
on
10828 - 스택
- 난이도: 실버 4
- 날짜: 2023년 2월 6일
- 상태: Correct
- 추가 검토 여부: No
solution
- 조건에 맞게 stl stack헤더를 사용하여 구현하였다.
code
#include <iostream>
#include <stack>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
string cmd;
int arg;
stack<int> stack;
for(int i = 0; i < N; i++)
{
cin >> cmd;
if(cmd == "push")
{
cin >> arg;
stack.push(arg);
}
else if(cmd == "pop")
{
if(stack.empty()) cout << -1 << endl;
else
{
cout << stack.top() << endl;
stack.pop();
}
}
else if(cmd == "size")
{
cout << stack.size() << endl;
}
else if(cmd == "empty")
{
if(stack.empty()) cout << 1 << endl;
else cout << 0 << endl;
}
else if(cmd == "top")
{
if(stack.empty()) cout << -1 << endl;
else
{
cout << stack.top() << endl;
}
}
}
return 0;
}