Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string>
  4. #include <stack>
  5. using namespace std;
  6.  
  7. stack<int> s;
  8. void push(int num) {
  9. s.push(num);
  10. }
  11.  
  12. void top() {
  13. if(s.empty()) printf("-1\n");
  14. else printf("%d\n", s.top());
  15. }
  16.  
  17. void size() {
  18. printf("%d\n", s.size());
  19. }
  20.  
  21. void empty() {
  22. if(s.empty()) printf("1\n");
  23. else printf("0\n");
  24. }
  25.  
  26. void pop() {
  27. if(s.empty()) printf("-1\n");
  28. else {
  29. top();
  30. s.pop();
  31. }
  32. }
  33.  
  34. int main(void)
  35. {
  36. int n;
  37. scanf("%d", &n);
  38.  
  39. while(n--) {
  40. string order;
  41. cin >> order;
  42.  
  43. if(order == "push") {
  44. int num;
  45. scanf("%d", &num);
  46.  
  47. push(num);
  48. }
  49. else if(order == "pop") pop();
  50. else if(order == "top") top();
  51. else if(order == "size") size();
  52. else if(order == "empty") empty();
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement