fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void push(int in);
  6. void pop();
  7. void size();
  8. void top();
  9. void empty();
  10.  
  11. int stack[10000] = { 0, };
  12. int len = 0;
  13.  
  14. int main()
  15. {
  16. int a;
  17.  
  18. scanf("%d", &a);
  19.  
  20. for(int i = 0; i < a; i++)
  21. {
  22. char oper[13];
  23.  
  24. int in;
  25.  
  26. gets(oper);
  27. strtok(oper, " ");
  28.  
  29. if(!strcmp(oper, "push"))
  30. {
  31. in = atoi(strtok(NULL, " "));
  32. push(in);
  33. }
  34. else if(!strcmp(oper, "pop"))
  35. {
  36. pop();
  37. }
  38. else if(!strcmp(oper, "size"))
  39. {
  40. size();
  41. }
  42. else if(!strcmp(oper, "empty"))
  43. {
  44. empty();
  45. }
  46. else if(!strcmp(oper, "top"))
  47. {
  48. top();
  49. }
  50.  
  51. }
  52.  
  53. return 0;
  54. }
  55.  
  56. void push(int in)
  57. {
  58. stack[len] = in;
  59. len++;
  60.  
  61. }
  62.  
  63. void pop()
  64. {
  65. if(stack[0] == 0)
  66. {
  67. printf("-1\n");
  68. }
  69. else
  70. {
  71. printf("%d\n", stack[len-1]);
  72. stack[len-1] = 0;
  73. if(len>0)
  74. {
  75. len--;
  76. }
  77. }
  78.  
  79.  
  80. }
  81.  
  82. void size()
  83. {
  84. if(stack[0] == 0)
  85. {
  86. printf("0\n");
  87. }
  88. else
  89. {
  90. printf("%d\n", len);
  91. }
  92. }
  93.  
  94. void empty()
  95. {
  96. if(stack[0] == 0)
  97. {
  98. printf("1\n");
  99. }
  100. else
  101. {
  102. printf("0\n");
  103. }
  104.  
  105. }
  106.  
  107. void top()
  108. {
  109. if(stack[0] == 0)
  110. {
  111. printf("-1\n");
  112. }
  113. else
  114. {
  115. printf("%d\n", stack[len-1]);
  116. }
  117.  
  118. }
Success #stdin #stdout 0s 4160KB
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