fork download
  1. #include <stdio.h>
  2. #define SIZE 5
  3.  
  4. int stack[SIZE];
  5. int sp;
  6.  
  7. void push(int value) {
  8. if (sp < SIZE) {
  9. stack[sp++] = value;
  10. } else {
  11. printf("Stack Overflow\n");
  12. }
  13. }
  14.  
  15. int pop(void) {
  16. if (sp > 0) {
  17. return stack[--sp];
  18. } else {
  19. printf("Stack Underflow\n");
  20. return -1;
  21. }
  22. }
  23.  
  24. int main(void) {
  25. sp = 0;
  26. int resp, data;
  27.  
  28. while (1) {
  29. printf("1:push 2:pop 0:end : ");
  30. scanf("%d", &resp);
  31.  
  32. if (!resp) break;
  33.  
  34. switch (resp) {
  35. case 1:
  36. printf("push : ");
  37. scanf("%d", &data);
  38. push(data);
  39. break;
  40. case 2:
  41. pop();
  42. break;
  43. }
  44.  
  45. printf("\n");
  46. for (int i = 0; i < sp; i++) {
  47. printf("stack[%d]=%d\n", i, stack[i]);
  48. }
  49. } // ← ここに while を閉じるカッコが必要です
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
1:push 2:pop 0:end :