C++语言模拟实现顺序队列方法一
Vivian 2018-06-19 来源 : 阅读 1101 评论 0

摘要:本文主要向大家介绍了C++语言模拟实现顺序队列方法,通过具体的实例让大家了解,希望对大家学习C++语言有所帮助。

本文主要向大家介绍了C++语言模拟实现顺序队列方法,通过具体的实例让大家了解,希望对大家学习C++语言有所帮助。

这是模拟实现顺序队列的第一种方法。

就是入队时队尾在不断后移,直到满,出队时队头不断后移,直到空。

 

如下如示例:

 C++语言模拟实现顺序队列方法一

 C++语言模拟实现顺序队列方法一C++语言模拟实现顺序队列方法一C++语言模拟实现顺序队列方法一

 

 

 

代码以及简单测试:

[cpp] view plain copy print?
1. #include <cassert> // assert();  
2. #include <iostream>  
3. using namespace std;  
4.   
5. // 头在不断变的顺序队列 (非循环队列)  
6. template<typename T>  
7. class Queue  
8. {  
9.       
10. public:  
11.     // 构造函数  
12.     Queue(const size_t capacity = 10)  
13.         :_rear(0)  
14.         ,_front(0)  
15.         ,_capacity(capacity)  
16.     {  
17.         _capacity = (_capacity >= 10) ? _capacity : 10; //最少分配十个元素的空间  
18.         _array = new T[_capacity];  
19.     }  
20.   
21.     // 拷贝构造  
22.     Queue(const Queue<T>& queue)  
23.     {  
24.         _Copying(queue);  
25.     }  
26.   
27.     // 赋值运算符重载  
28.     Queue<T>& operator=(const Queue<T>& queue)  
29.     {  
30.         if (this != &queue)  
31.         {  
32.             Queue tempQueue(queue);  
33.             _Copying(tempQueue);  
34.         }  
35.         return *this;  
36.     }  
37.   
38.     // 入队  
39.     void Push(const T&data)  
40.     {  
41.         assert(!Full());  
42.         _array[_rear++] = data;  
43.     }  
44.   
45.     // 出队  
46.     void Pop()  
47.     {  
48.         assert( !Empty() );  
49.         _front++;  
50.     }  
51.   
52.     // 队头  
53.     T& Front()  
54.     {  
55.         assert(!Empty());  
56.         return _array[_front];  
57.     }  
58.   
59.     const T& Front()const  
60.     {  
61.         assert(!Empty());  
62.         return _array[_front];  
63.     }  
64.   
65.     // 队尾  
66.     T& Back()  
67.     {  
68.         assert(!<span style="font-family:Arial, Helvetica, sans-serif;">Empty</span>());  
69.         return _array[_rear-1];  
70.     }  
71.   
72.     const T& Back()const  
73.     {  
74.         assert(!Empty());  
75.         return _array[_rear-1];  
76.     }  
77.   
78.     // 当前队列中元素个数  
79.     size_t Length()const  
80.     {  
81.         return _rear - _front;  
82.     }  
83.   
84.     // 析构函数  
85.     ~Queue()  
86.     {  
87.         if (NULL != _array )  
88.         {  
89.             delete[] _array; _array = NULL;  
90.             _front = 0;  
91.             _rear = 0;  
92.             _capacity = 0;  
93.         }  
94.     }  
95.   
96.     // 判断是否满  
97.     bool Full()const  
98.     {  
99.         return _rear == _capacity;  
100.     }  
101.   
102.     // 判断是否为空  
103.     bool Empty()const  
104.     {  
105.         return 0 == _rear;                                                                                                                                              _capacity;  
106.     }  
107.   
108. private:  
109.     // 赋值和拷贝构造 调用  
110.     void _Copying(const Queue<T>& queue)  
111.     {  
112.         _array = new T[queue._capacity];  
113.         _rear = queue._rear;  
114.         _capacity = queue._capacity;  
115.         _front = queue._front;  
116.   
117.         for (size_t idx = _front; idx < _rear; ++idx )  
118.         {  
119.             _array[idx] = queue._array[idx];  
120.         }  
121.     }  
122. private:  
123.     T* _array; // 指向数据的数组  
124.     size_t _rear; // 队尾  
125.     size_t _front; // 对头  
126.     size_t _capacity; // 队列容量  
127. };  
128.   
129.   
130. //  测试  
131. void Test1()  
132. {  
133.     // 测试基本操作  
134.     Queue<int> queue1;  
135.     queue1.Push(1);  
136.     cout << "插入一个元素后:" <<endl;  
137.     cout << "空否? " << queue1.Empty() << "长度? " << queue1.Length() << endl;  
138.     queue1.Push(2);  
139.     queue1.Push(3);  
140.     queue1.Push(4);  
141.     cout << "又入队三个元素后:" <<endl;  
142.     cout << "空否? " << queue1.Empty() << "长度? " << queue1.Length() << endl;  
143.     cout << "满否? " << queue1.Full() << "队头? " << queue1.Front() << "队尾?" << queue1.Back() <<endl;  
144.     queue1.Pop();  
145.     queue1.Pop();  
146.     cout <<"出队两个个后,队头?" << queue1.Front() << "队尾 " << queue1.Back() << endl;  
147.   
148.     // 测试拷贝构造和赋值运算符重载  
149. //  Queue<int> queue2;  
150. //  queue2.Push(1);  
151. //  queue2.Push(2);  
152. //  queue2.Push(3);  
153. //  queue2.Push(4);  
154. //  // Queue<int> que_copy_q2(queue2);  
155. //   
156. //  Queue<int> que_copy_q2;  
157. //  que_copy_q2 = queue2;  
158. //  cout << "queue2 :"<< endl;  
159. //  cout << "队头:" << queue2.Front() << "队尾:" <<queue2.Back() << "空吗:" << boolalpha << queue2.Empty() << "长度:" << queue2.Length() <<endl;  
160. //  cout << "根据queue2 拷贝而来的队列:"<< endl;  
161. //  cout << "队头:" << que_copy_q2.Front() << "队尾:" <<que_copy_q2.Back() << "空吗:" << boolalpha << que_copy_q2.Empty()<< "长度:" << queue2.Length() <<endl;  
162.   
163. }  
164.   
165.   
166. int main()  
167. {  
168.     Test1();  
169.     return 0;  
170. }

 

 

1.测试基本操作 

 

 C++语言模拟实现顺序队列方法一


2.测试拷贝和赋值运算

 C++语言模拟实现顺序队列方法一

本文由职坐标整理并发布,了解更多内容,请关注职坐标编程语言C/C+频道!

本文由 @Vivian 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程