fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. int arr[10001];
  6. int index;
  7.  
  8. void push(int number)
  9. {
  10. arr[index] = number;
  11. index++;
  12. }
  13. int size()
  14. {
  15. return index;
  16. }
  17.  
  18. int top()
  19. {
  20. return (!size()) ? -1 : arr[index - 1];
  21. }
  22.  
  23. bool empty()
  24. {
  25. return (!size()) ? true : false;
  26. }
  27.  
  28. void pop()
  29. {
  30. if (!size())
  31. cout << -1 << endl;
  32. else
  33. {
  34. cout << arr[index - 1] << endl;
  35. index--;
  36. }
  37. }
  38. int main(void)
  39. {
  40. string com; //명령어
  41. int num; //push할 정수
  42. int n; //테스트 케이스 수
  43. cin >> n;
  44.  
  45. while (n--)
  46. {
  47. cin >> com;
  48.  
  49. if ("push" == com)
  50. {
  51. cin >> num; //push일 경우에만 정수 입력
  52. push(num);
  53. }
  54. else if ("top" == com)
  55. cout << top() << endl;
  56. else if ("size" == com)
  57. cout << size() << endl;
  58. else if ("empty" == com)
  59. cout << empty() << endl;
  60. else if ("pop" == com)
  61. pop();
  62. }
  63. return 0;
  64. }
Success #stdin #stdout 0s 4708KB
stdin
14
push 1
push 2
top
size
empty
pop
pop
pop
size
empty
pop
push 3
empty
top
stdout
2
2
0
2
1
-1
0
1
-1
0
3