fork download
  1. #include <stdio.h>
  2.  
  3. #define MAX 100
  4.  
  5. int queue[MAX];
  6. int front = 0;
  7. int rear = 0;
  8.  
  9. /* enQueue関数 */
  10. int enQueue(int data) {
  11. printf("enQueue開始時: front = %d, rear = %d\n", front, rear);
  12.  
  13. if (rear >= MAX) {
  14. return -1; // 満杯
  15. }
  16.  
  17. queue[rear] = data;
  18. rear++;
  19.  
  20. return 0;
  21. }
  22.  
  23. /* deQueue関数 */
  24. int deQueue(int *data) {
  25. printf("deQueue開始時: front = %d, rear = %d\n", front, rear);
  26.  
  27. if (front == rear) {
  28. return -1; // 空
  29. }
  30.  
  31. *data = queue[front];
  32. front++;
  33.  
  34. return 0;
  35. }
  36.  
  37. /* キューの内容を表示する関数 */
  38. void printQueue() {
  39. int i;
  40.  
  41. printf("Queue = [ ");
  42. for (i = front; i < rear; i++) {
  43. printf("%d ", queue[i]);
  44. }
  45. printf("]\n");
  46. }
  47.  
  48. /* main関数 */
  49. int main() {
  50. int x;
  51.  
  52. enQueue(10);
  53. enQueue(20);
  54. enQueue(30);
  55.  
  56. printQueue();
  57.  
  58. deQueue(&x);
  59. printf("取り出した値 = %d\n", x);
  60.  
  61. printQueue();
  62.  
  63. return 0;
  64. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
enQueue開始時: front = 0, rear = 0
enQueue開始時: front = 0, rear = 1
enQueue開始時: front = 0, rear = 2
Queue = [ 10 20 30 ]
deQueue開始時: front = 0, rear = 3
取り出した値 = 10
Queue = [ 20 30 ]