fork download
  1. #include <stdio.h>
  2. #define SIZE 5
  3. int stack[SIZE];
  4. int sp;
  5. void push(int value);
  6. int pop(void);
  7.  
  8. int main(void) {
  9. sp=0;
  10. int resp, data;
  11.  
  12. while(1){
  13. printf("1:push 2:pop 0:end:");
  14. scanf("%d", &resp);
  15.  
  16. if(!resp)break;
  17.  
  18. switch(resp){
  19. case 1:
  20. printf("push : ");
  21. scanf("%d", &data);
  22. push(data);
  23. break;
  24. case 2:
  25. 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. return 0;
  35. }
  36.  
  37. void push(int value){
  38. if(sp >= SIZE){
  39. printf("スタックが満杯で入りませんでした\n");
  40. }else{
  41. stack[sp++] = value;
  42. }
  43. }
  44.  
  45. int pop(void){
  46. if(sp<=0){
  47. printf("スタックが空で取り出せませんでした\n");
  48. return 0;
  49. }else{
  50. return stack[--sp];
  51. }
  52. }
Success #stdin #stdout 0s 5316KB
stdin
0 5 0 2 0 3 1 1 0 2 1 1 0 1 0 2 0 3 0 4 0 5 0 6
stdout
1:push 2:pop 0:end: