fork download
  1. #include <stdio.h>
  2.  
  3. #define SIZE 5
  4. int queue[SIZE];
  5. int head, tail;
  6.  
  7.  
  8. void enqueue(int value);
  9. int dequeue(void);
  10.  
  11. int main(void){
  12.  
  13. head = tail = 0;
  14. int data, i;
  15.  
  16.  
  17. enqueue(1);
  18. enqueue(2);
  19. dequeue();
  20. enqueue(3);
  21. dequeue();
  22. enqueue(4);
  23. enqueue(5);
  24. dequeue();
  25. enqueue(6);
  26.  
  27.  
  28.  
  29. for(i = 0; i < SIZE; i++){
  30. printf("queue[%d]=%d\n", i, queue[i]);
  31. }
  32.  
  33. return 0;
  34. }
  35.  
  36.  
  37. void enqueue(int value)
  38. {
  39.  
  40. if(head == (tail + 1) % SIZE){
  41. printf("キューは満杯です: %dを格納できません\n", value);
  42. return;
  43. }
  44.  
  45.  
  46. queue[tail] = value;
  47.  
  48.  
  49. tail = (tail + 1) % SIZE;
  50. }
  51.  
  52.  
  53. int dequeue(void)
  54. {
  55. int data;
  56.  
  57.  
  58. if (head == tail) {
  59. printf("キューは空です\n");
  60. return -1;
  61. }
  62.  
  63.  
  64. data = queue[head];
  65.  
  66.  
  67. queue[head] = 0;
  68.  
  69.  
  70. head = (head + 1) % SIZE;
  71.  
  72. return data;
  73. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
queue[0]=6
queue[1]=0
queue[2]=0
queue[3]=4
queue[4]=5