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.  
  19.  
  20.  
  21.  
  22.  
  23. for(i=0; i<SIZE; i++){
  24. printf("queue[%d]=%d\n", i, queue[i] );
  25. }
  26.  
  27. return 0;
  28. }
  29.  
  30. void enqueue(int value)
  31. {
  32. if(head==(tail+1)%SIZE){
  33. printf("キューは満杯で入りませんでした\n");
  34. }else{
  35. queue[tail]=value;
  36. tail++;
  37. }
  38. tail=tail&SIZE;
  39. }
  40.  
  41. int dequeue(void)
  42. {
  43. int value;
  44. if(head==tail){
  45. printf("キューは空で取り出せませんでした\n");
  46. return 0;
  47. }else{
  48. value=queue[head];
  49. queue[head]=0;
  50. head++;
  51. }
  52. head=head%SIZE;
  53. return value;
  54. }
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
キューは空で取り出せませんでした
queue[0]=1
queue[1]=2
queue[2]=0
queue[3]=0
queue[4]=0