fork download
  1. #include <stdio.h>
  2.  
  3. #define SIZE 5
  4. int queue[SIZE];
  5. int head, tail, count;
  6.  
  7. void enqueue(int value);
  8. int dequeue(void);
  9. int isFull(void);
  10. int isEmpty(void);
  11. void display(void);
  12. void initialize(void);
  13.  
  14. int main(void)
  15. {
  16. initialize();
  17. int resp, data;
  18.  
  19. while(1){
  20. printf("1:enqueue 2:dequeue 3:initialize 0:end : "); scanf("%d", &resp);
  21.  
  22. if(!resp) break;
  23.  
  24. switch(resp){
  25. case 1:printf("enqueue : "); scanf("%d", &data);
  26. enqueue(data);
  27. break;
  28. case 2:dequeue();
  29. break;
  30. case 3:initialize();
  31. break;
  32. }
  33. printf("head=%d, tail=%d\n", head, tail);
  34. }
  35.  
  36. display();
  37.  
  38. return 0;
  39. }
  40.  
  41. void enqueue(int value)
  42. {
  43. if( isFull() ){
  44. printf("キューは満杯で入りませんでした\n");
  45. }else{
  46. queue[tail++] = value;
  47. tail = tail % SIZE;
  48. }
  49. }
  50.  
  51. int dequeue(void)
  52. {
  53. int value;
  54. if( isEmpty() ){
  55. printf("キューは空で取り出せませんでした\n");
  56. return 0;
  57. }else{
  58. value = queue[head++];
  59. head = head % SIZE;
  60. return value;
  61. }
  62. }
  63.  
  64. int isFull(void)
  65. {
  66. if( head == (tail+1)%SIZE ){
  67. return 1;
  68. }else{
  69. return 0;
  70. }
  71. }
  72.  
  73. int isEmpty(void)
  74. {
  75. if( head == tail ){
  76. return 1;
  77. }else{
  78. return 0;
  79. }
  80. }
  81.  
  82. void display(void)
  83. {
  84. printf("\n");
  85. for(int i=0; i<count; i++){
  86. printf("queue[%d]=%d\n",(head+i)%SIZE, queue[(head+i)%SIZE]);
  87. }
  88. }
  89.  
  90. void initialize(void)
  91. {
  92. head = tail = count = 0;
  93. }
Success #stdin #stdout 0.01s 5320KB
stdin
1 2 3 4 5 0
stdout
1:enqueue  2:dequeue  3:initialize  0:end : enqueue : head=0, tail=1
1:enqueue  2:dequeue  3:initialize  0:end : head=0, tail=0
1:enqueue  2:dequeue  3:initialize  0:end : head=0, tail=0
1:enqueue  2:dequeue  3:initialize  0:end : head=0, tail=0
1:enqueue  2:dequeue  3:initialize  0:end :