zoukankan      html  css  js  c++  java
  • 链式队列

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





  • 相关阅读:
    HTTP 深入详解(HTTP Web 的基础)
    webpack 代码分离
    webpack 常见问题
    细说 webpack 之流程篇
    一个页面从输入 URL 到页面加载显示完成,这个过程中都发生了什么?
    Ajax 解决浏览器缓存问题
    十大经典排序算法
    react-redux 之 connect 方法详解
    JS实现继承的几种方式
    GIT常用命令及常见问题解决方法-协作篇
  • 原文地址:https://www.cnblogs.com/zhuzhenfeng/p/4626557.html
Copyright © 2011-2022 走看看