Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
82
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. using namespace std;
  5.  
  6. int arr[10001];
  7. int idx = 0;
  8.  
  9. void push(int num) {
  10. arr[idx++] = num;
  11. }
  12.  
  13. void top() {
  14. if(idx == 0) printf("-1\n");
  15. else printf("%d\n", arr[idx - 1]);
  16. }
  17.  
  18. void size() {
  19. printf("%d\n", idx);
  20. }
  21.  
  22. void empty() {
  23. if(idx == 0) printf("1\n");
  24. else printf("0\n");
  25. }
  26.  
  27. void pop() {
  28. if(idx == 0) printf("-1\n");
  29. else {
  30. top();
  31. idx--;
  32. }
  33. }
  34.  
  35. int main(void)
  36. {
  37. int n;
  38. scanf("%d", &n);
  39.  
  40. while(n--) {
  41. string order;
  42. cin >> order;
  43.  
  44. if(order == "push") {
  45. int num;
  46. scanf("%d", &num);
  47.  
  48. push(num);
  49. }
  50. else if(order == "pop") pop();
  51. else if(order == "top") top();
  52. else if(order == "size") size();
  53. else if(order == "empty") empty();
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement