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. int isEmpty(void);
  11. void display(void);
  12. void initialize(void);
  13.  
  14. int main(void)
  15. {
  16. initialize();
  17. int resp, data;
  18.  
  19. while(1){
  20. printf("1:push 2:pop 3:initialize 0:end : \n");
  21. scanf("%d", &resp);
  22.  
  23. if(resp == 0) break;
  24.  
  25. switch(resp){
  26. case 1:printf("pushする値:"); scanf("%d", &data);
  27. push(data);
  28. break;
  29. case 2:pop();
  30. break;
  31. case 3:initialize();
  32. break;
  33. }
  34. }
  35.  
  36. printf("sp=%d\n", sp);
  37. display();
  38. return 0;
  39. }
  40.  
  41.  
  42. void push(int value)
  43. {
  44. if( isFull() ){
  45. printf("スタックが満杯で入りませんでした\n");
  46. } else {
  47. stack[sp++] = value;
  48. }
  49. }
  50.  
  51. int pop(void)
  52. {
  53. if( isEmpty() ){
  54. printf("スタックが空で取り出せませんでした\n");
  55. return 0;
  56. } else {
  57. return stack[--sp];
  58. }
  59. }
  60.  
  61. int isFull(void)
  62. {
  63. if(sp >= SIZE){
  64. return 1;
  65. } else {
  66. return 0;
  67. }
  68. }
  69.  
  70. int isEmpty(void)
  71. {
  72. if(sp <= 0){
  73. return 1;
  74. } else {
  75. return 0;
  76. }
  77. }
  78.  
  79. void display(void)
  80. {
  81. printf("\n");
  82. for(int i = 0; i < sp; i++){
  83. printf("stack[%d]=%d\n", i, stack[i]);
  84. }
  85. }
  86.  
  87. void initialize(void)
  88. {
  89. sp = 0;
  90. }
Success #stdin #stdout 0.01s 5276KB
stdin
1 2
2
0
stdout
1:push  2:pop  3:initialize  0:end : 
pushする値:1:push  2:pop  3:initialize  0:end : 
1:push  2:pop  3:initialize  0:end : 
sp=0