Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. const int MAX = 10000 + 1;
  6.  
  7. int idx;
  8. int arr[MAX];
  9.  
  10. // stl 사용하지 않고 구현
  11. int top(void)
  12. {
  13. return arr[idx - 1];
  14. }
  15.  
  16. void push(int num)
  17. {
  18. arr[idx++] = num;
  19. }
  20.  
  21. void pop(void)
  22. {
  23. idx--;
  24. }
  25.  
  26. int main(void)
  27. {
  28. ios_base::sync_with_stdio(0);
  29. cin.tie(0);
  30. int N;
  31. cin >> N;
  32.  
  33. for (int n = 0; n < N; n++)
  34. {
  35. string s;
  36. cin >> s;
  37.  
  38. if (s == "push")
  39. {
  40. int num;
  41. cin >> num;
  42.  
  43. push(num);
  44. }
  45. else if (s == "pop")
  46. {
  47. if (idx)
  48. {
  49. cout << top() << "\n";
  50. pop();
  51. }
  52. else
  53. cout << -1 << "\n";
  54. }
  55. else if (s == "size")
  56. {
  57. cout << idx << "\n";
  58. }
  59. else if (s == "empty")
  60. {
  61. bool flag = (idx == 0);
  62. cout << flag << "\n";
  63. }
  64. else
  65. {
  66. if (idx)
  67. cout << top() << "\n";
  68. else
  69. cout << -1 << "\n";
  70. }
  71. }
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement