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. void enqueue(int value)
  11. {
  12. if (head == (tail + 1) % SIZE) {
  13. printf("満杯です\n");
  14. } else {
  15. queue[tail] = value;
  16. tail = (tail + 1) % SIZE;
  17. }
  18. }
  19.  
  20. int dequeue(void)
  21. {
  22. int value;
  23. if (head == tail) {
  24. printf("空です\n");
  25. return -1;
  26. } else {
  27. value = queue[head];
  28. queue[head] = 0;
  29. head = (head + 1) % SIZE;
  30. return value;
  31. }
  32. }
  33.  
  34. int main(void)
  35. {
  36. head = tail = 0;
  37. int data, i;
  38.  
  39. enqueue(1);
  40. enqueue(2);
  41. dequeue();
  42. enqueue(3);
  43. dequeue();
  44. enqueue(4);
  45. enqueue(5);
  46. dequeue();
  47. enqueue(6);
  48.  
  49. for (i = 0; i < SIZE; i++) {
  50. printf("queue[%d] = %d\n", i, queue[i]);
  51. }
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0s 5260KB
stdin
Standard input is empty
stdout
queue[0] = 6
queue[1] = 0
queue[2] = 0
queue[3] = 4
queue[4] = 5