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. int isFull(void);
  10.  
  11. int main(void)
  12. {
  13. sp = 0;
  14. int resp, data;
  15.  
  16. while(1){
  17. printf("1:push 2:pop 0:end : ");
  18. scanf("%d", &resp);
  19.  
  20. if(!resp) break;
  21.  
  22. switch(resp){
  23. case 1:
  24. printf("push : ");
  25. scanf("%d", &data);
  26. push(data);
  27. break;
  28. case 2:
  29. pop();
  30. break;
  31. }
  32. printf("sp=%d\n", sp);
  33. }
  34.  
  35. printf("\n");
  36. for(int i=0; i<sp; i++){
  37. printf("stack[%d]=%d\n", i, stack[i]);
  38. }
  39.  
  40. return 0;
  41. }
  42.  
  43. int isFull(void)
  44. {
  45. return sp >= SIZE;
  46. }
  47.  
  48. void push(int value)
  49. {
  50. if(isFull()){
  51. printf("スタックが満杯で入りませんでした\n");
  52. }else{
  53. stack[sp++] = value;
  54. }
  55. }
  56.  
  57. int pop(void)
  58. {
  59. if(sp <= 0){
  60. printf("スタックが空で取り出せませんでした\n");
  61. return 0;
  62. }else{
  63. return stack[--sp];
  64. }
  65. }
  66.  
Success #stdin #stdout 0s 5276KB
stdin
Standard input is empty
stdout
1:push 2:pop 0:end : push : sp=1
1:push 2:pop 0:end : push : sp=2
1:push 2:pop 0:end : sp=1
1:push 2:pop 0:end : sp=1
1:push 2:pop 0:end : push : sp=2
1:push 2:pop 0:end : 
stack[0]=10
stack[1]=99