摘要:本文主要向大家介绍了C++语言模拟实现顺序队列方法,通过具体的实例让大家了解,希望对大家学习C++语言有所帮助。
本文主要向大家介绍了C++语言模拟实现顺序队列方法,通过具体的实例让大家了解,希望对大家学习C++语言有所帮助。
这是模拟实现顺序队列的第二种方法。
就是队头保持不变,队尾在变。入队时,队尾向后走,出队时将除队头以外的所有数据向前移动一步。
如下如示例:
1.代码示例
[cpp] view plain copy print? 1. // 顺序队列实现方法2 (非循环队列) 对头保持不变 2. #include <cassert> 3. #include <iostream> 4. using namespace std; 5. 6. template <typename T> 7. class Queue 8. { 9. public: 10. // 构造函数 11. Queue(const size_t capacity = 10) 12. :_rear(0) 13. ,_capacity(capacity) 14. { 15. _capacity = _capacity > 10 ? _capacity : 10; 16. _array = new T[_capacity]; 17. } 18. 19. // 拷贝构造 20. Queue(const Queue<T>& que) 21. { 22. _array = new T[que._capacity]; 23. _capacity = que._capacity; 24. _rear = que._rear; 25. for (size_t idx = 0; idx < _rear; ++idx) 26. { 27. _array[idx] = que._array[idx]; 28. } 29. } 30. 31. // 赋值运算符重载 32. Queue<T>& operator=(const Queue<T>& que) 33. { 34. if (this != &que) 35. { 36. Queue<T> temp_que(que); 37. std::swap(_array, temp_que._array); 38. _capacity = que._capacity; 39. _rear = que._rear; 40. } 41. return *this; 42. } 43. 44. // 入队 45. void Push(const T& data) 46. { 47. assert(!Full()); // 为假时成了 即队列满 48. _array[_rear++] = data; 49. } 50. // 出队 51. void Pop() 52. { 53. assert(!Empty()); 54. for (size_t idx = 0; idx < _rear - 1; ++idx) 55. { 56. _array[idx] = _array[idx + 1]; 57. } 58. _rear--; 59. } 60. 61. // 元素个数 62. size_t Length()const 63. { 64. return _rear; 65. } 66. // 队头 67. T& Front() 68. { 69. assert(!Empty()); 70. return _array[0]; 71. } 72. const T& Front()const 73. { 74. assert(!Empty()); 75. return _array[0]; 76. } 77. 78. // 队尾 79. T& Back() 80. { 81. assert(!Empty()); 82. return _array[_rear - 1]; 83. } 84. const T& Back()const 85. { 86. assert(!Empty()); 87. return _array[_rear - 1]; 88. } 89. 90. // 判满 91. bool Full()const 92. { 93. return _rear == _capacity; 94. } 95. 96. // 判空 97. bool Empty()const 98. { 99. return 0 == _rear; 100. } 101. // 析构函数 102. ~Queue() 103. { 104. if (NULL != _array) 105. { 106. delete[] _array; 107. _array = NULL; 108. _rear = 0; 109. _capacity = 0; 110. } 111. } 112. 113. private: 114. T* _array; 115. size_t _rear; // 队尾 116. size_t _capacity; //容量 117. }; 118. 119. int main() 120. { 121. // Queue<int> q1; 122. // q1.Front(); 123. // q1.Back(); 124. 125. // Queue<int> q2; 126. // cout << q2.Length() << endl; 127. // q2.Push(1); 128. // q2.Push(2); 129. // q2.Push(3); 130. // cout << q2.Length() << endl; 131. // cout << q2.Empty() << " " << q2.Full() << " " << q2.Front() << " " << q2.Back() << " " << endl; 132. 133. // Queue<int> q3; 134. // q3.Push(1); 135. // q3.Push(2); 136. // q3.Push(3); 137. // cout << q3.Length() << endl; 138. // q3.Pop(); 139. // q3.Pop(); 140. // cout << q3.Length() <<" " << q3.Front() <<endl; 141. 142. Queue<int> q4; 143. q4.Push(1); 144. q4.Push(2); 145. q4.Push(3); 146. q4.Push(4); 147. //Queue<int> q5(q4); 148. Queue<int> q5; 149. q5 = q4; 150. cout << q5.Front() << " " << q5.Back() <<" " << q5.Length()<<" " << q5.Empty() <<" " << q5.Full() <<endl; 151. return 0; 152. }
这次实现的顺序队列虽然没有在 模拟实现顺序队列方法一 中所实现队列的“假溢出”现象,但每次出队需要移动大量元素,效率有些低,在 用三种方法模拟实现循环队列 中将实现。
本文由职坐标整理并发布,了解更多内容,请关注职坐标编程语言C/C+频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号