fork download
  1. #include<stdio.h>
  2. #define SIZE 5
  3. int queue[SIZE];
  4. int head, tail;
  5. void enqueue(int value);
  6. int dequeue(void);
  7. int main(void)
  8. {
  9. head = tail = 0;
  10. int i;
  11. enqueue(1);
  12. enqueue(2);
  13. dequeue();
  14. enqueue(3);
  15. dequeue();
  16. enqueue(4);
  17. enqueue(5);
  18. dequeue();
  19. enqueue(6);
  20. for(i=0; i<SIZE; i++){
  21. printf("queue[%d]=%d\n", i, queue[i] );
  22. }
  23. return 0;
  24. }
  25. void enqueue(int value){
  26. if(head==(tail+1)){
  27. printf("入れません\n");
  28. }else {
  29. queue[tail++] =value;
  30. }
  31. tail = tail % SIZE;
  32.  
  33. }
  34. int dequeue(void){
  35. int value;
  36. if(head == tail){
  37. printf("空で取れない。\n");
  38. return 0;
  39. }else{
  40. value= queue[head];
  41. queue[head++]=0;
  42. }
  43. head = head%SIZE;
  44. return value;
  45. }
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
queue[0]=6
queue[1]=0
queue[2]=0
queue[3]=4
queue[4]=5