fork download
  1. #include<stdio.h>
  2.  
  3. #define SIZE 5
  4. int queue[SIZE];
  5. int head, tail;
  6.  
  7. void enqueue(int value);
  8. int dequeue(void);
  9.  
  10. int main(void)
  11. {
  12. head = tail = 0;
  13. int data, i;
  14.  
  15. enqueue(1);
  16. enqueue(2);
  17. dequeue();
  18. enqueue(3);
  19. dequeue();
  20. enqueue(4);
  21. enqueue(5);
  22. dequeue();
  23. enqueue(6);
  24. for(i=0; i<SIZE; i++){
  25. printf("queue[%d]=%d\n", i, queue[i] );
  26. }
  27. return 0;
  28. }
  29. void enqueue(int value)
  30. {
  31. if ((tail + 1) % SIZE == head) {
  32. printf("キューは満杯です\n");
  33. return;
  34. }
  35. queue[tail] = value;
  36. tail = (tail + 1) % SIZE;
  37. }
  38. int dequeue(void)
  39. {
  40. int value;
  41. if (head == tail) {
  42. printf("キューは空です\n");
  43. return -1;
  44. }
  45. value = queue[head];
  46. queue[head] = 0;
  47. head = (head + 1) % SIZE;
  48. return value;
  49. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
queue[0]=6
queue[1]=0
queue[2]=0
queue[3]=4
queue[4]=5