ZBLOG

#include #include #include using namespace std; // 函数结果状态代码 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define OVERFLOW -1 typedef int QElemType; typedef struct QNode { QElemType data; ...

这段代码是一个基于链表实现的队列的示例,包含了队列的初始化、销毁、清空、判空、求长度等操作。以下是完整代码的注释部分:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. using namespace std;
  5. #define TRUE 1
  6. #define FALSE 0
  7. #define OK 1
  8. #define ERROR 0
  9. #define OVERFLOW -1
  10. typedef int QElemType;
  11. typedef struct QNode {
  12. QElemType data;
  13. struct QNode *next;
  14. } *QueuePtr;
  15. struct LinkQueue {
  16. QueuePtr front, rear; // 队头、队尾指针
  17. };
  18. void print(QElemType i) {
  19. printf("%d ", i);
  20. }
  21. void InitQueue(LinkQueue &Q); // 构造一个空队列Q
  22. void DestroyQueue(LinkQueue &Q); // 销毁队列Q,Q不再存在
  23. void ClearQueue(LinkQueue &Q); // 将Q清为空队列
  24. int QueueEmpty(LinkQueue Q); // 若队列Q为空队列,则返回TRUE;否则返回FALSE
  25. int QueueLength(LinkQueue Q); // 返回Q的元素个数,即队列的长度
  26. int GetHead(LinkQueue Q, QElemType &e); // 若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR
  27. int EnQueue(LinkQueue &Q, QElemType e); // 插入元素e为Q的新的队尾元素
  28. int DeQueue(LinkQueue &Q, QElemType &e); // 若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
  29. void QueueTraverse(LinkQueue Q, void(*vi)(QElemType)); // 从队头到队尾依次对队列Q中每个元素调用函数vi()
  30. int main() {
  31. int j;
  32. int i = 0, l;
  33. QElemType d;
  34. LinkQueue Q;
  35. InitQueue(Q);
  36. for (i = 0; i < 5; i++) {
  37. scanf("%d", &d);
  38. EnQueue(Q, d);
  39. };
  40. printf("队列长度为: %d\n", QueueLength(Q));
  41. printf("现在队列中元素:\n");
  42. QueueTraverse(Q, print);
  43. DeQueue(Q, d);
  44. printf("删除的元素是%d\n", d);
  45. DeQueue(Q, d);
  46. printf("删除的元素是%d\n", d);
  47. scanf("%d", &d);
  48. EnQueue(Q,d);
  49. printf("队列长度为: %d\n", QueueLength(Q));
  50. printf("现在队列中元素:\n");
  51. QueueTraverse(Q, print);
  52. j = GetHead(Q, d);
  53. if (j) {
  54. printf("现在队头元素为:%d\n", d);
  55. }
  56. ClearQueue(Q);
  57. DestroyQueue(Q);
  58. }
  59. void InitQueue(LinkQueue &Q) {
  60. // 构造一个空队列Q
  61. Q.front = Q.rear = new QNode;
  62. if (!Q.front) exit(OVERFLOW); // 存储分配失败
  63. Q.front->next = NULL;
  64. }
  65. void DestroyQueue(LinkQueue &Q) {
  66. // 销毁队列Q(无论空否均可)
  67. while (Q.front) {
  68. Q.rear = Q.front->next;
  69. delete Q.front;
  70. Q.front = Q.rear;
  71. }
  72. }
  73. void ClearQueue(LinkQueue &Q) {
  74. // 将Q清为空队列
  75. QueuePtr p, q;
  76. Q.rear = Q.front; // 队尾指针也指向头结点,使链表中无任何元素
  77. p = Q.front->next; // 指向首元结点
  78. while (p) { // 遍历删除所有结点
  79. q = p->next;
  80. delete p;
  81. p = q;
  82. }
  83. Q.front->next = NULL; // 头结点的指针域置空
  84. }
  85. int QueueEmpty(LinkQueue Q) {
  86. // 若Q为空队列,则返回TRUE,否则返回FALSE
  87. if (Q.front == Q.rear) return TRUE;
  88. else return FALSE;
  89. }
  90. int QueueLength(LinkQueue Q) {
  91. // 求队列的长度
  92. int len = 0;
  93. QueuePtr p = Q.front->next; // 指向首元结点
  94. while (p) {
  95. len++;
  96. p = p->next;
  97. }
  98. return len;
  99. }
  100. int GetHead(LinkQueue Q, QElemType &e) {
  101. // 若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR
  102. if (Q.front == Q.rear)
  103. return ERROR; // 队列为空,无法获取队头元素
  104. e = Q.front->next->data; // 获取队头元素的值
  105. return OK;
  106. }
  107. int EnQueue(LinkQueue &Q, QElemType e) {
  108. // 插入元素e为Q的新的队尾元素
  109. QueuePtr p = new QNode;
  110. if (!p) exit(OVERFLOW); // 存储分配失败
  111. p->data = e;
  112. p->next = NULL;
  113. Q.rear->next = p; // 新结点插入队尾
  114. Q.rear = p; // 队尾指针指向新结点
  115. return OK;
  116. }
  117. int DeQueue(LinkQueue &Q, QElemType &e) {
  118. // 若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
  119. if (Q.front == Q.rear)
  120. return ERROR; // 队列为空,无法删除元素
  121. QueuePtr p = Q.front->next; // 指向首元结点
  122. e = p->data; // 获取队头元素的值
  123. Q.front->next = p->next; // 头结点的后继指针指向第二个结点
  124. if (Q.rear == p) // 如果删除后队列为空,修改队尾指针
  125. Q.rear = Q.front;
  126. delete p; // 释放原队头结点的空间
  127. return OK;
  128. }
  129. void QueueTraverse(LinkQueue Q, void(*vi)(QElemType)) {
  130. // 从队头到队尾依次对队列Q中每个元素调用函数vi()
  131. QueuePtr p = Q.front->next; // 指向首元结点
  132. while (p) {
  133. vi(p->data);
  134. p = p->next;
  135. }
  136. }

以上代码实现了一个链式队列,包含了初始化队列、销毁队列、清空队列、判断队列是否为空、获取队头元素、插入元素到队尾以及删除队头元素等操作。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=9462

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?