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