Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <string>
- #include <stack>
- using namespace std;
- stack<int> s;
- void push(int num) {
- s.push(num);
- }
- void top() {
- if(s.empty()) printf("-1\n");
- else printf("%d\n", s.top());
- }
- void size() {
- printf("%d\n", s.size());
- }
- void empty() {
- if(s.empty()) printf("1\n");
- else printf("0\n");
- }
- void pop() {
- if(s.empty()) printf("-1\n");
- else {
- top();
- s.pop();
- }
- }
- int main(void)
- {
- int n;
- scanf("%d", &n);
- while(n--) {
- string order;
- cin >> order;
- if(order == "push") {
- int num;
- scanf("%d", &num);
- push(num);
- }
- else if(order == "pop") pop();
- else if(order == "top") top();
- else if(order == "size") size();
- else if(order == "empty") empty();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement