• 循环队列实现


    1. #pragma once
    2. #include
    3. #include
    4. using namespace std;
    5. template <typename T>
    6. class CircularQueue
    7. {
    8. private:
    9. vector data; //储存队列的数组
    10. int head; //指向队列起始位置的指针
    11. int tail; //指向队列结束位置的指针
    12. int size; //数组的大小
    13. public:
    14. CircularQueue(); //默认构造函数
    15. CircularQueue(int k); //构造函数
    16. bool isEmpty(); //判断队列是否为空
    17. bool isFull(); //判断队列是否已满
    18. bool enQueue(T value); //入队,若成功则返回true,反之为false
    19. bool deQueue(T& value); //出队,若成功则返回false,反之为false
    20. int Front(); //返回队列起始位置的值
    21. int Back(); //返回队列结束位置的值
    22. int Size(); //返回队列的大小
    23. void show(); //显示队列的所有值
    24. };
    25. template <typename T>
    26. CircularQueue::CircularQueue() //默认构造函数
    27. {
    28. data.resize(0);
    29. head = -1;
    30. tail = -1;
    31. size = 0;
    32. }
    33. template <typename T>
    34. CircularQueue::CircularQueue(int k) //构造函数
    35. {
    36. data.resize(k);
    37. head = -1;
    38. tail = -1;
    39. size = k;
    40. }
    41. template <typename T>
    42. bool CircularQueue::isEmpty() //判断队列是否为空
    43. {
    44. return head == -1;
    45. }
    46. template <typename T>
    47. bool CircularQueue::isFull() //判断队列是否已满
    48. {
    49. return((tail + 1) % size) == head;
    50. }
    51. template <typename T>
    52. bool CircularQueue::enQueue(T value) //入队,若成功则返回true,反之为false
    53. {
    54. if (isFull())
    55. {
    56. cout << "超出范围啦!" << endl;
    57. return false;
    58. }
    59. if (isEmpty())
    60. head = 0;
    61. tail = (tail + 1) % size;
    62. data[tail] = value;
    63. return true;
    64. }
    65. template <typename T>
    66. bool CircularQueue::deQueue(T& value) //出队,若成功则返回false,反之为false
    67. {
    68. if (isEmpty())
    69. {
    70. cout << "队列已经空啦!" << endl;
    71. return false;
    72. }
    73. if (head == tail)
    74. {
    75. head = -1;
    76. tail = -1;
    77. return false;
    78. }
    79. value = data[head];
    80. head = (head + 1) % size;
    81. return true;
    82. }
    83. template <typename T>
    84. int CircularQueue::Front() //返回队列起始位置的值
    85. {
    86. if (isEmpty())
    87. return -1;
    88. return data[head];
    89. }
    90. template <typename T>
    91. int CircularQueue::Back() //返回队列结束位置的值
    92. {
    93. if (isEmpty())
    94. return -1;
    95. return data[tail];
    96. }
    97. template <typename T>
    98. int CircularQueue::Size() //返回队列的大小
    99. {
    100. if (isEmpty())
    101. return 0;
    102. else
    103. {
    104. if (head < tail)
    105. return tail - head + 1;
    106. if (head > tail)
    107. return tail - head + size + 1;
    108. if (head == tail)
    109. return 1;
    110. }
    111. }
    112. template <typename T>
    113. void CircularQueue::show() //显示队列的所有值
    114. {
    115. if (isEmpty())
    116. cout << "队列为空" << endl;
    117. else
    118. {
    119. if (head < tail)
    120. {
    121. cout << "head ->";
    122. for (int i = head; i < tail; i++)
    123. cout << data[i] << ",";
    124. cout << data[tail] << "<- tail" << endl;
    125. }
    126. if (head > tail)
    127. {
    128. cout << "head ->";
    129. for (int i = head; i < size; i++)
    130. cout << data[i] << ",";
    131. for (int i = 0; i < tail; i++)
    132. cout << data[i] << ",";
    133. cout << data[tail] << "<- tail" << endl;
    134. }
    135. if (head == tail)
    136. cout << "head ->" << data[head] << "<- tail" << endl;
    137. }
    138. }

  • 相关阅读:
    浅浅的整理一下机器学习简单资料
    【OpenCV】Mat矩阵解析 Mat类赋值,单/双/三通道 Mat赋值
    指数移动平均EMA
    软件测试行业35岁职场魔咒,你准备怎么应对?
    前端面试基础面试题——6
    7.25模拟赛总结
    缓存穿透、缓存击穿、缓存雪崩
    金仓数据库KingbaseES物理备份恢复命令选项(expire命令)
    HTML+CSS简单的网页制作期末作业 关于我的家乡——四川文化网页介绍 DW大学生网页作业制作设计 Dreamweaver简单网页成品
    Python-Flask快速上手
  • 原文地址:https://blog.csdn.net/qq_27085429/article/details/99676121