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. head = tail = 0;
  12. int resp, data, i;
  13.  
  14. while(1){
  15. printf("\n1: enqueue 2: dequeue 0: end → ");
  16. scanf("%d", &resp);
  17.  
  18. if(!resp) break;
  19.  
  20. switch(resp){
  21. case 1:
  22. printf("enqueue → ");
  23. scanf("%d", &data);
  24. enqueue(data);
  25. break;
  26. case 2:
  27. dequeue();
  28. break;
  29. default:
  30. printf("入力エラーです。\n");
  31. break;
  32. }
  33.  
  34. printf("head=%d, tail=%d\n", head, tail);
  35.  
  36. i = head;
  37. while(i != tail){
  38. printf("queue[%d] = %d\n", i, queue[i]);
  39. i = (i + 1) % SIZE;
  40. }
  41. }
  42.  
  43. return 0;
  44. }
  45.  
  46. void enqueue(int value){
  47. int next = (tail + 1) % SIZE;
  48.  
  49. if(next == head){
  50. printf("キューが満杯です!\n");
  51. return;
  52. }
  53.  
  54. queue[tail] = value;
  55. tail = next;
  56. printf("%d を追加しました。\n", value);
  57. }
  58.  
  59. int dequeue(void){
  60. if(head == tail){
  61. printf("キューが空です!\n");
  62. return -1;
  63. }
  64.  
  65. int value = queue[head];
  66. head = (head + 1) % SIZE;
  67. printf("%d を取り出しました。\n", value);
  68. return value;
  69. }
  70.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
1: enqueue  2: dequeue  0: end →