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 : ");
  21. scanf("%d", &resp);
  22.  
  23. if(!resp) 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. printf("sp=%d\n", sp);
  35. }
  36.  
  37. display();
  38.  
  39. return 0;
  40. }
  41. void push(int value)
  42. {
  43. if( isFull() ){
  44. printf("スタックが満杯で入りませんでした\n");
  45. }else{
  46. stack[sp++] = value;
  47. }
  48. }
  49.  
  50. int pop(void)
  51. {
  52. if( isEmpty() ){
  53. printf("スタックが空で取り出せませんでした\n");
  54. return 0;
  55. }else{
  56. return stack[--sp];
  57. }
  58. }
  59.  
  60. int isFull(void)
  61. {
  62. if(sp >= SIZE){
  63. return 1;
  64. }else{
  65. return 0;
  66. }
  67. }
  68.  
  69. int isEmpty(void)
  70. {
  71. if(sp <= 0){
  72. return 1;
  73. }else{
  74. return 0;
  75. }
  76. }
  77.  
  78. void display(void)
  79. {
  80. printf("\n");
  81. for(int i=0; i<sp; i++){
  82. printf("stack[%d]=%d\n", i, stack[i]);
  83. }
  84. }
  85.  
  86. void initialize(void)
  87. {
  88. sp = 0;
  89. }
Success #stdin #stdout 0.01s 5308KB
stdin
1 5 1 3 2 2 0
stdout
1:push 2:pop 3:initialize 0:end : push : sp=1
1:push 2:pop 3:initialize 0:end : push : sp=2
1:push 2:pop 3:initialize 0:end : sp=1
1:push 2:pop 3:initialize 0:end : sp=0
1:push 2:pop 3:initialize 0:end :