1. #include "stdio.h"
    2. #include "stdlib.h"
    3. #include "io.h"
    4. #include "math.h"
    5. #include "time.h"
    6. #define OK 1
    7. #define ERROR 0
    8. #define TRUE 1
    9. #define FALSE 0
    10. #define MAXSIZE 20 /* 存储空间初始分配量 */
    11. typedef int Status;
    12. typedef int QElemType; /* QElemType类型根据实际情况而定,这里假设为int */
    13. /* 循环队列的顺序存储结构 */
    14. typedef struct
    15. {
    16. QElemType data[MAXSIZE];
    17. int front; /* 头指针 */
    18. int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
    19. }SqQueue;
    20. Status visit(QElemType c)
    21. {
    22. printf("%d ",c);
    23. return OK;
    24. }
    25. /* 初始化一个空队列Q */
    26. Status InitQueue(SqQueue *Q)
    27. {
    28. Q->front=0;
    29. Q->rear=0;
    30. return OK;
    31. }
    32. /* 将Q清为空队列 */
    33. Status ClearQueue(SqQueue *Q)
    34. {
    35. Q->front=Q->rear=0;
    36. return OK;
    37. }
    38. /* 若队列Q为空队列,则返回TRUE,否则返回FALSE */
    39. Status QueueEmpty(SqQueue Q)
    40. {
    41. if(Q.front==Q.rear) /* 队列空的标志 */
    42. return TRUE;
    43. else
    44. return FALSE;
    45. }
    46. /* 返回Q的元素个数,也就是队列的当前长度 */
    47. int QueueLength(SqQueue Q)
    48. {
    49. return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
    50. }
    51. /* 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR */
    52. Status GetHead(SqQueue Q,QElemType *e)
    53. {
    54. if(Q.front==Q.rear) /* 队列空 */
    55. return ERROR;
    56. *e=Q.data[Q.front];
    57. return OK;
    58. }
    59. /* 若队列未满,则插入元素e为Q新的队尾元素 */
    60. Status EnQueue(SqQueue *Q,QElemType e)
    61. {
    62. if ((Q->rear+1)%MAXSIZE == Q->front) /* 队列满的判断 */
    63. return ERROR;
    64. Q->data[Q->rear]=e; /* 将元素e赋值给队尾 */
    65. Q->rear=(Q->rear+1)%MAXSIZE;/* rear指针向后移一位置, */
    66. /* 若到最后则转到数组头部 */
    67. return OK;
    68. }
    69. /* 若队列不空,则删除Q中队头元素,用e返回其值 */
    70. Status DeQueue(SqQueue *Q,QElemType *e)
    71. {
    72. if (Q->front == Q->rear) /* 队列空的判断 */
    73. return ERROR;
    74. *e=Q->data[Q->front]; /* 将队头元素赋值给e */
    75. Q->front=(Q->front+1)%MAXSIZE; /* front指针向后移一位置, */
    76. /* 若到最后则转到数组头部 */
    77. return OK;
    78. }
    79. /* 从队头到队尾依次对队列Q中每个元素输出 */
    80. Status QueueTraverse(SqQueue Q)
    81. {
    82. int i;
    83. i=Q.front;
    84. while((i+Q.front)!=Q.rear)
    85. {
    86. visit(Q.data[i]);
    87. i=(i+1)%MAXSIZE;
    88. }
    89. printf("\n");
    90. return OK;
    91. }
    92. int main()
    93. {
    94. Status j;
    95. int i=0,l;
    96. QElemType d;
    97. SqQueue Q;
    98. InitQueue(&Q);
    99. printf("初始化队列后,队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
    100. printf("请输入整型队列元素(不超过%d个),-1为提前结束符: ",MAXSIZE-1);
    101. do
    102. {
    103. /* scanf("%d",&d); */
    104. d=i+100;
    105. if(d==-1)
    106. break;
    107. i++;
    108. EnQueue(&Q,d);
    109. }while(i<MAXSIZE-1);
    110. printf("队列长度为: %d\n",QueueLength(Q));
    111. printf("现在队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
    112. printf("连续%d次由队头删除元素,队尾插入元素:\n",MAXSIZE);
    113. for(l=1;l<=MAXSIZE;l++)
    114. {
    115. DeQueue(&Q,&d);
    116. printf("删除的元素是%d,插入的元素:%d \n",d,l+1000);
    117. /* scanf("%d",&d); */
    118. d=l+1000;
    119. EnQueue(&Q,d);
    120. }
    121. l=QueueLength(Q);
    122. printf("现在队列中的元素为: \n");
    123. QueueTraverse(Q);
    124. printf("共向队尾插入了%d个元素\n",i+MAXSIZE);
    125. if(l-2>0)
    126. printf("现在由队头删除%d个元素:\n",l-2);
    127. while(QueueLength(Q)>2)
    128. {
    129. DeQueue(&Q,&d);
    130. printf("删除的元素值为%d\n",d);
    131. }
    132. j=GetHead(Q,&d);
    133. if(j)
    134. printf("现在队头元素为: %d\n",d);
    135. ClearQueue(&Q);
    136. printf("清空队列后, 队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
    137. return 0;
    138. }