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