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; #define MAX_QSIZE 5 // 最大队列长度+1 struct SqQu...

补充完整的代码如下所示:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. using namespace std;
  5. // 函数结果状态代码
  6. #define TRUE 1
  7. #define FALSE 0
  8. #define OK 1
  9. #define ERROR 0
  10. #define OVERFLOW -1
  11. typedef int QElemType;
  12. #define MAX_QSIZE 5 // 最大队列长度+1
  13. struct SqQueue
  14. {
  15. QElemType *base; // 初始化的动态分配存储空间
  16. int front; // 头指针,若队列不空,指向队列头元素
  17. int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置
  18. };
  19. void print(QElemType i)
  20. {
  21. printf("%d ",i);
  22. }
  23. void InitQueue(SqQueue &Q); // 构造一个空队列Q
  24. void DestroyQueue(SqQueue &Q); // 销毁队列Q,Q不再存在
  25. void ClearQueue(SqQueue &Q); // 将Q清为空队列
  26. int QueueEmpty(SqQueue Q); // 若队列Q为空队列,则返回TRUE;否则返回FALSE
  27. int QueueLength(SqQueue Q); // 返回Q的元素个数,即队列的长度
  28. int GetHead(SqQueue Q, QElemType &e); // 若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR
  29. int EnQueue(SqQueue &Q, QElemType e); // 插入元素e为Q的新的队尾元素
  30. int DeQueue(SqQueue &Q, QElemType &e); // 若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
  31. void QueueTraverse(SqQueue Q, void(*vi)(QElemType)); // 从队头到队尾依次对队列Q中每个元素调用函数vi()
  32. int main()
  33. {
  34. int j;
  35. int i=0,l;
  36. QElemType d;
  37. SqQueue Q;
  38. InitQueue(Q);
  39. for(i=0; i<MAX_QSIZE; i++)
  40. {
  41. scanf("%d", &d);
  42. EnQueue(Q, d);
  43. };
  44. printf("队列长度为: %d\n", QueueLength(Q));
  45. printf("现在队列中元素:\n");
  46. QueueTraverse(Q, print);
  47. DeQueue(Q, d);
  48. printf("删除的元素是%d\n", d);
  49. DeQueue(Q, d);
  50. printf("删除的元素是%d\n", d);
  51. scanf("%d", &d);
  52. EnQueue(Q, d);
  53. printf("队列长度为: %d\n", QueueLength(Q));
  54. printf("现在队列中元素:\n");
  55. QueueTraverse(Q, print);
  56. j = GetHead(Q, d);
  57. if(j)
  58. printf("现在队头元素为:%d\n", d);
  59. ClearQueue(Q);
  60. DestroyQueue(Q);
  61. return 0;
  62. }
  63. // 循环队列的基本操作(9个)
  64. void InitQueue(SqQueue &Q)
  65. { // 构造一个空队列Q
  66. /********** Begin **********/
  67. Q.base = (QElemType*)malloc(sizeof(QElemType) * MAX_QSIZE); // 动态分配存储空间
  68. if(!Q.base) exit(OVERFLOW);
  69. Q.front = Q.rear = 0; // 初始化队头和队尾指针为0
  70. /********** End **********/
  71. }
  72. void DestroyQueue(SqQueue &Q)
  73. { // 销毁队列Q,Q不再存在
  74. /********** Begin **********/
  75. free(Q.base); // 释放动态分配的存储空间
  76. Q.base = NULL;
  77. Q.front = Q.rear = 0; // 队头和队尾指针置零
  78. /********** End **********/
  79. }
  80. void ClearQueue(SqQueue &Q)
  81. { // 将Q清为空队列
  82. /********** Begin **********/
  83. Q.front = Q.rear = 0; // 队头和队尾指针置零即可实现清空操作
  84. /********** End **********/
  85. }
  86. int QueueEmpty(SqQueue Q)
  87. { // 若队列Q为空队列,则返回TRUE;否则返回FALSE
  88. /********** Begin **********/
  89. if(Q.front == Q.rear) return TRUE;
  90. else return FALSE;
  91. /********** End **********/
  92. }
  93. int QueueLength(SqQueue Q)
  94. { // 返回Q的元素个数,即队列的长度
  95. /********** Begin **********/
  96. return (Q.rear - Q.front + MAX_QSIZE) % MAX_QSIZE;
  97. /********** End **********/
  98. }
  99. int GetHead(SqQueue Q, QElemType &e)
  100. { // 若队列不空,则用e返回Q的对头元素,并返回OK;否则返回ERROR
  101. /********** Begin **********/
  102. if(QueueEmpty(Q)) return ERROR;
  103. e = Q.base[Q.front];
  104. return OK;
  105. /********** End **********/
  106. }
  107. int EnQueue(SqQueue &Q, QElemType e)
  108. { // 插入元素e为Q的新的队尾元素
  109. /********** Begin **********/
  110. if((Q.rear + 1) % MAX_QSIZE == Q.front) return ERROR; // 队满,无法插入
  111. Q.base[Q.rear] = e; // 将新元素放入队尾
  112. Q.rear = (Q.rear + 1) % MAX_QSIZE; // 队尾指针后移一位
  113. return OK;
  114. /********** End **********/
  115. }
  116. int DeQueue(SqQueue &Q, QElemType &e)
  117. { // 若队列不空,则删除Q的对头元素,用e返回其值,并返回OK;否则返回ERROR
  118. /********** Begin **********/
  119. if(QueueEmpty(Q)) return ERROR; // 队空,无法删除
  120. e = Q.base[Q.front]; // 将队头元素赋值给e
  121. Q.front = (Q.front + 1) % MAX_QSIZE; // 队头指针后移一位
  122. return OK;
  123. /********** End **********/
  124. }
  125. void QueueTraverse(SqQueue Q, void(*vi)(QElemType))
  126. { // 从队头到队尾依次对队列Q中每个元素调用函数vi()  
  127. int i;
  128. i = Q.front;
  129. while(i != Q.rear)
  130. {
  131. vi(Q.base[i]);
  132. i = (i + 1) % MAX_QSIZE;
  133. }
  134. }

这是一个简单的循环队列的实现,包含了初始化队列、销毁队列、清空队列、判断队列是否为空、获取队头元素、入队操作、出队操作以及遍历队列等基本操作。你可以根据需要修改代码,并进行测试运行。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?