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