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.  
  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. {if( head == (tail+1)%SIZE ){
  34. printf("キューは満杯で入りませんでした\n");
  35. }else{
  36. queue[tail] = value;
  37. tail = tail + 1;
  38. }
  39. tail = tail % SIZE;
  40. }
  41.  
  42. int dequeue(void)
  43. {int value;
  44. if( head == tail ){
  45. printf("キューは空で取り出せませんでした\n");
  46. return 0;
  47. }else{
  48. value = queue[head];
  49. queue[head] = 0; //抽出後に0を代入
  50. head = head + 1;
  51. }
  52. head = head % SIZE;
  53. return value;
  54. }
  55.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
queue[0]=6
queue[1]=0
queue[2]=0
queue[3]=4
queue[4]=5