Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- const int MAX = 10000 + 1;
- int idx;
- int arr[MAX];
- // stl 사용하지 않고 구현
- int top(void)
- {
- return arr[idx - 1];
- }
- void push(int num)
- {
- arr[idx++] = num;
- }
- void pop(void)
- {
- idx--;
- }
- int main(void)
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int N;
- cin >> N;
- for (int n = 0; n < N; n++)
- {
- string s;
- cin >> s;
- if (s == "push")
- {
- int num;
- cin >> num;
- push(num);
- }
- else if (s == "pop")
- {
- if (idx)
- {
- cout << top() << "\n";
- pop();
- }
- else
- cout << -1 << "\n";
- }
- else if (s == "size")
- {
- cout << idx << "\n";
- }
- else if (s == "empty")
- {
- bool flag = (idx == 0);
- cout << flag << "\n";
- }
- else
- {
- if (idx)
- cout << top() << "\n";
- else
- cout << -1 << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement