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. void display(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: printf("push : "); scanf("%d", &data);
  24. push( data );
  25. break;
  26. case 2: printf( "pop : %d ", pop() );
  27. break;
  28. }
  29. printf("sp=%d\n", sp);
  30. }
  31. printf("\n");
  32. display();
  33.  
  34. return 0;
  35. }
  36.  
  37. void push(int value)
  38. {
  39. if(sp >= SIZE){
  40. printf("スタックが満杯で入りませんでした\n");
  41. }else{
  42. stack[sp++] = value;
  43. }
  44. }
  45.  
  46. int pop(void)
  47. {
  48. if(sp <= 0){
  49. printf("スタックが空で取り出せませんでした\n");
  50. return 0;
  51. }else{
  52. return stack[--sp];
  53. }
  54. }
  55.  
  56. void display(void){
  57. for(int i=0; i<sp; i++){
  58. printf("stack[%d]=%d \n", i, stack[i]);
  59. }
  60. }
  61.  
Success #stdin #stdout 0s 5456KB
stdin
1  10  1  20  1  30  2  0
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 : push : sp=3
1:push 2:pop 0:end : pop : 30 sp=2
1:push 2:pop 0:end : 
stack[0]=10 
stack[1]=20