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