fork download
  1. #include<stdio.h>
  2.  
  3. #define SIZE 5
  4. int stack[SIZE];
  5. int sp;
  6.  
  7. void push(int value);
  8. int pop(void);
  9.  
  10. int main(void)
  11. {
  12. sp=0;
  13. int resp,data;
  14.  
  15. while(1)
  16. {
  17. scanf("%d",&resp);
  18. if(resp==0)
  19. break;
  20.  
  21. switch(resp)
  22. {
  23. case 1:scanf("%d",&data);
  24. push(data);
  25. break;
  26. case 2:pop();
  27. break;
  28. }
  29. printf("sp=%d\n",sp);
  30. }
  31. printf("\n");
  32. for(int i=0;i<sp;i++)
  33. {
  34. printf("stack[%d]=%d\n",i,stack[i]);
  35. }
  36. return 0;
  37. }
  38.  
  39. void push(int value)
  40. {
  41. if(sp>=SIZE)
  42. {
  43. printf("NOT");
  44. }
  45. else
  46. {
  47. stack[sp++]=value;
  48. }
  49. }
  50.  
  51. int pop(void)
  52. {
  53. if(sp<=0)
  54. {
  55. printf("not");
  56. return 0;
  57. }
  58. else
  59. {
  60. return stack[--sp];
  61. }
  62. }
  63.  
Success #stdin #stdout 0.01s 5516KB
stdin
1
2
1
3
0
stdout
sp=1
sp=2

stack[0]=2
stack[1]=3