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. typedef struct QNode /* 结点结构 */
    14. {
    15. QElemType data;
    16. struct QNode *next;
    17. }QNode,*QueuePtr;
    18. typedef struct /* 队列的链表结构 */
    19. {
    20. QueuePtr front,rear; /* 队头、队尾指针 */
    21. }LinkQueue;
    22. Status visit(QElemType c)
    23. {
    24. printf("%d ",c);
    25. return OK;
    26. }
    27. /* 构造一个空队列Q */
    28. Status InitQueue(LinkQueue *Q)
    29. {
    30. Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
    31. if(!Q->front)
    32. exit(OVERFLOW);
    33. Q->front->next=NULL;
    34. return OK;
    35. }
    36. /* 销毁队列Q */
    37. Status DestroyQueue(LinkQueue *Q)
    38. {
    39. while(Q->front)
    40. {
    41. Q->rear=Q->front->next;
    42. free(Q->front);
    43. Q->front=Q->rear;
    44. }
    45. return OK;
    46. }
    47. /* 将Q清为空队列 */
    48. Status ClearQueue(LinkQueue *Q)
    49. {
    50. QueuePtr p,q;
    51. Q->rear=Q->front;
    52. p=Q->front->next;
    53. Q->front->next=NULL;
    54. while(p)
    55. {
    56. q=p;
    57. p=p->next;
    58. free(q);
    59. }
    60. return OK;
    61. }
    62. /* 若Q为空队列,则返回TRUE,否则返回FALSE */
    63. Status QueueEmpty(LinkQueue Q)
    64. {
    65. if(Q.front==Q.rear)
    66. return TRUE;
    67. else
    68. return FALSE;
    69. }
    70. /* 求队列的长度 */
    71. int QueueLength(LinkQueue Q)
    72. {
    73. int i=0;
    74. QueuePtr p;
    75. p=Q.front;
    76. while(Q.rear!=p)
    77. {
    78. i++;
    79. p=p->next;
    80. }
    81. return i;
    82. }
    83. /* 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR */
    84. Status GetHead(LinkQueue Q,QElemType *e)
    85. {
    86. QueuePtr p;
    87. if(Q.front==Q.rear)
    88. return ERROR;
    89. p=Q.front->next;
    90. *e=p->data;
    91. return OK;
    92. }
    93. /* 插入元素e为Q的新的队尾元素 */
    94. Status EnQueue(LinkQueue *Q,QElemType e)
    95. {
    96. QueuePtr s=(QueuePtr)malloc(sizeof(QNode));
    97. if(!s) /* 存储分配失败 */
    98. exit(OVERFLOW);
    99. s->data=e;
    100. s->next=NULL;
    101. Q->rear->next=s; /* 把拥有元素e的新结点s赋值给原队尾结点的后继,见图中① */
    102. Q->rear=s; /* 把当前的s设置为队尾结点,rear指向s,见图中② */
    103. return OK;
    104. }
    105. /* 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR */
    106. Status DeQueue(LinkQueue *Q,QElemType *e)
    107. {
    108. QueuePtr p;
    109. if(Q->front==Q->rear)
    110. return ERROR;
    111. p=Q->front->next; /* 将欲删除的队头结点暂存给p,见图中① */
    112. *e=p->data; /* 将欲删除的队头结点的值赋值给e */
    113. Q->front->next=p->next;/* 将原队头结点的后继p->next赋值给头结点后继,见图中② */
    114. if(Q->rear==p) /* 若队头就是队尾,则删除后将rear指向头结点,见图中③ */
    115. Q->rear=Q->front;
    116. free(p);
    117. return OK;
    118. }
    119. /* 从队头到队尾依次对队列Q中每个元素输出 */
    120. Status QueueTraverse(LinkQueue Q)
    121. {
    122. QueuePtr p;
    123. p=Q.front->next;
    124. while(p)
    125. {
    126. visit(p->data);
    127. p=p->next;
    128. }
    129. printf("\n");
    130. return OK;
    131. }
    132. int main()
    133. {
    134. int i;
    135. QElemType d;
    136. LinkQueue q;
    137. i=InitQueue(&q);
    138. if(i)
    139. printf("成功地构造了一个空队列!\n");
    140. printf("是否空队列?%d(1:空 0:否) ",QueueEmpty(q));
    141. printf("队列的长度为%d\n",QueueLength(q));
    142. EnQueue(&q,-5);
    143. EnQueue(&q,5);
    144. EnQueue(&q,10);
    145. printf("插入3个元素(-5,5,10)后,队列的长度为%d\n",QueueLength(q));
    146. printf("是否空队列?%d(1:空 0:否) ",QueueEmpty(q));
    147. printf("队列的元素依次为:");
    148. QueueTraverse(q);
    149. i=GetHead(q,&d);
    150. if(i==OK)
    151. printf("队头元素是:%d\n",d);
    152. DeQueue(&q,&d);
    153. printf("删除了队头元素%d\n",d);
    154. i=GetHead(q,&d);
    155. if(i==OK)
    156. printf("新的队头元素是:%d\n",d);
    157. ClearQueue(&q);
    158. printf("清空队列后,q.front=%u q.rear=%u q.front->next=%u\n",q.front,q.rear,q.front->next);
    159. DestroyQueue(&q);
    160. printf("销毁队列后,q.front=%u q.rear=%u\n",q.front, q.rear);
    161. return 0;
    162. }