当前位置:   article > 正文

数据结构-循环队列的基本操作代码实现_编写循环代码,实现输入10个数据进队列

编写循环代码,实现输入10个数据进队列

数据结构复习day5)

  1. #include <iostream>
  2. using namespace std;
  3. typedef int ElemType;
  4. const int QUEUE_INIT_SIZE = 100;
  5. const int QUEUEINCREMENT = 10;
  6. typedef struct {
  7. ElemType* data;
  8. int front;
  9. int rear;
  10. int queuesize;
  11. int incresize;
  12. }SqQueue;
  13. bool InitQueue(SqQueue& Q, int = QUEUE_INIT_SIZE, int = QUEUEINCREMENT);//初始化循环队列
  14. int QueueLength(SqQueue Q);//返回队列长度
  15. bool DeQueue(SqQueue& Q, ElemType& e);//将队首元素出队,用e返回
  16. bool EnQueue(SqQueue& Q, ElemType e);//将元素e放入循环队列
  17. bool GetHead(SqQueue Q, ElemType& e);//取队首元素,用e返回
  18. bool incrementQueuesize(SqQueue& Q);//当循环队列空间不足时,动态扩充空间
  19. void TraverseQueue(SqQueue Q);//遍历队列
  20. bool QueueEmpty(SqQueue Q);//判断队列是否为空
  21. bool ClearQueue(SqQueue& Q);//清空队列
  22. bool InitQueue(SqQueue& Q, int maxsize, int incresize) {
  23. Q.data = new ElemType[maxsize];
  24. if (!Q.data)return 0;
  25. Q.front = Q.rear = 0;
  26. Q.queuesize = maxsize;
  27. Q.incresize = incresize;
  28. return 1;
  29. }
  30. int QueueLength(SqQueue Q) {
  31. return (Q.rear - Q.front + Q.queuesize) % Q.queuesize;
  32. }
  33. bool DeQueue(SqQueue& Q, ElemType& e) {
  34. if (Q.front == Q.rear)
  35. return 0;
  36. e = Q.data[Q.front];
  37. Q.front = (Q.front + 1) % Q.queuesize;
  38. return 1;
  39. }
  40. bool EnQueue(SqQueue& Q, ElemType e) {
  41. if ((Q.rear + 1) % Q.queuesize == Q.front)
  42. if (!incrementQueuesize(Q))
  43. return 0;
  44. Q.data[Q.rear] = e;
  45. Q.rear = (Q.rear + 1) % Q.queuesize;
  46. return 1;
  47. }
  48. bool GetHead(SqQueue Q, ElemType& e) {
  49. if (Q.rear == Q.front)
  50. return 0;
  51. e = Q.data[Q.front];
  52. return 1;
  53. }
  54. bool incrementQueuesize(SqQueue& Q) {
  55. ElemType* newdata = new ElemType[Q.queuesize + Q.incresize];
  56. if (!newdata)return 0;
  57. for (int i = 0; i < Q.queuesize; i++)
  58. newdata[i] = Q.data[(Q.front + i) % Q.queuesize];
  59. delete[] Q.data;
  60. Q.data = newdata;
  61. Q.front = 0; Q.rear = Q.queuesize - 1;
  62. Q.queuesize += Q.incresize;
  63. return 1;
  64. }
  65. void TraverseQueue(SqQueue Q) {
  66. while (Q.front != Q.rear) {
  67. cout << Q.data[Q.front] << " ";
  68. Q.front = (Q.front + 1) % Q.queuesize;
  69. }
  70. cout << endl;
  71. }
  72. bool QueueEmpty(SqQueue Q) {
  73. if (Q.front == Q.rear)
  74. return 1;
  75. return 0;
  76. }
  77. bool ClearQueue(SqQueue& Q) {
  78. Q.front = Q.rear;
  79. return 1;
  80. }
  81. int main()
  82. {
  83. SqQueue Q;
  84. ElemType e;
  85. int i;
  86. if (InitQueue(Q, 8, 2))
  87. cout << "初始化循环队列成功(8个元素空间)" << endl;
  88. else {
  89. cout << "初始化循环队列失败" << endl;
  90. return 1;
  91. }
  92. cout << "请输入10个入队元素:" << endl;
  93. for (i = 0; i < 10; i++) {
  94. cin >> e;
  95. EnQueue(Q, e);
  96. }
  97. cout << "队列长度为" << QueueLength(Q);
  98. cout << "队列中元素为:";
  99. TraverseQueue(Q);
  100. cout << "出队5个元素" << endl;
  101. for (i = 0; i < 5; i++)
  102. DeQueue(Q, e);
  103. cout << "队列长度为" << QueueLength(Q) << endl;
  104. cout << "队列中元素为:";
  105. TraverseQueue(Q);
  106. cout << "请再输入6个入队元素:" << endl;
  107. for (i = 0; i < 6; i++) {
  108. cin >> e;
  109. EnQueue(Q, e);
  110. }
  111. cout << "队列长度为" << QueueLength(Q) << endl;
  112. cout << "队列中元素为:";
  113. TraverseQueue(Q);
  114. ClearQueue(Q);
  115. if (QueueEmpty(Q))
  116. cout << "队列已清空" << endl;
  117. return 0;
  118. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/412746
推荐阅读
相关标签
  

闽ICP备14008679号