链式队列

 
  
  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("\n");
  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("成功地构造了一个空队列!\n");
  139. printf("是否空队列?%d(1:空 0:否) ", QueueEmpty(q));
  140. printf("队列的长度为%d\n", QueueLength(q));
  141. EnQueue(&q, -5);
  142. EnQueue(&q, 5);
  143. EnQueue(&q, 10);
  144. printf("插入3个元素(-5,5,10)后,队列的长度为%d\n", 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\n", d);
  151. DeQueue(&q, &d);
  152. printf("删除了队头元素%d\n", d);
  153. i = GetHead(q, &d);
  154. if (i == OK)
  155. printf("新的队头元素是:%d\n", d);
  156. ClearQueue(&q);
  157. printf("清空队列后,q.front=%u q.rear=%u q.front->next=%u\n", q.front, q.rear, q.front->next);
  158. DestroyQueue(&q);
  159. printf("销毁队列后,q.front=%u q.rear=%u\n", q.front, q.rear);
  160. return 0;
  161. }





转载于:https://www.cnblogs.com/zhuzhenfeng/p/4626557.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值