树  0.1
数据结构_第6章
vecQueue.hh
Go to the documentation of this file.
1 /****************************************************
2  * @file vecQueue.hh
3  * @author Guorui Wei (313017602@qq.com)
4  * @brief 队头位置固定的顺序队列的定义和实现
5  * @version 0.1
6  * @date 2020-04-06
7  *
8  * @copyright Copyright (c) 2020
9  *
10  * See the file LICENSE in the top directory of this distribution for
11  * more information.
12  *
13  ****************************************************/
14 
15 #ifndef INCLUDE_VECQUEUE_HH_
16 #define INCLUDE_VECQUEUE_HH_
17 
18 #include "Queue.h"
19 #include <utility>
20 
25 namespace Queue
26 {
27 
33 template <typename T>
34 class vecQueue : public Queue<T>
35 {
36 public:
43  typedef T value_type;
44  typedef value_type &reference;
45  typedef const value_type &const_reference;
46  typedef size_t size_type;
47 private:
53 
59 
67 
72  void doubleSpace();
73 
74 public:
80  vecQueue(size_type initSize = 10);
81 
86  virtual ~vecQueue();
87 
94  virtual bool isEmpty() const
95  {
96  return empty();
97  }
98 
104  virtual void enQueue(const_reference &x)
105  {
106  push(x);
107  }
108 
114  virtual value_type deQueue()
115  {
116  value_type retVal = front();
117  pop();
118  return retVal;
119  }
120 
126  virtual value_type getHead() const
127  {
128  return front();
129  }
130 
137  bool empty() const;
138 
144  size_type size() const;
145 
151  reference &front();
152 
158  const_reference &front() const;
159 
165  reference &back();
166 
172  const_reference &back() const;
173 
180  void push(const value_type &val);
181 
188  void push(value_type &&val);
189 
195  void pop();
196 };
197 
198 template <class T>
199 vecQueue<T>::vecQueue(size_type initSize)
200  : _maxSize(initSize), _data(new value_type[initSize]), _rear(0)
201 {
202 }
203 
204 template <class T>
206 {
207  delete[] _data;
208 }
209 
210 template <class T>
211 bool vecQueue<T>::empty() const
212 {
213  return !_rear;
214 }
215 
216 template <class T>
218 {
219  return _rear;
220 }
221 
222 template <class T>
224 {
225  return _data[0];
226 }
227 
228 template <class T>
230 {
231  return _data[0];
232 }
233 
234 template <class T>
236 {
237  return _data[_rear - 1];
238 }
239 
240 template <class T>
242 {
243  return _data[_rear - 1];
244 }
245 
246 template <class T>
248 {
249  if (_rear == _maxSize)
250  doubleSpace();
251 
252  _data[_rear++] = val;
253 }
254 
255 template <class T>
256 void vecQueue<T>::push(value_type &&val)
257 {
258  if (_rear == _maxSize)
259  doubleSpace();
260 
261  _data[_rear++] = std::move(val);
262 }
263 
264 template <class T>
265 void vecQueue<T>::pop()
266 {
267  --_rear;
268  for (size_type i = 0; i < _rear; ++i)
269  _data[i] = _data[i + 1];
270 }
271 
272 template <class T>
274 {
275  value_type *old = _data;
276  _data = new value_type[_maxSize = 2 * _maxSize + 1];
277  for (size_type i = 0; i < _rear; ++i)
278  _data[i] = old[i];
279  delete[] old;
280 }
281 
282 } // namespace Queue
283 
284 #endif /* INCLUDE_VECQUEUE_HH_ */
Queue::vecQueue::size
size_type size() const
Returns the number of elements in the queue.
Definition: vecQueue.hh:229
Queue::vecQueue::getHead
virtual value_type getHead() const
Get the Head object.
Definition: vecQueue.hh:150
Queue::vecQueue::deQueue
virtual value_type deQueue()
出队一个元素
Definition: vecQueue.hh:138
Queue::vecQueue::isEmpty
virtual bool isEmpty() const
判队空
Definition: vecQueue.hh:118
Queue::vecQueue::enQueue
virtual void enQueue(const_reference &x)
入队一个元素
Definition: vecQueue.hh:128
Queue::vecQueue::doubleSpace
void doubleSpace()
扩大数组空间
Definition: vecQueue.hh:285
Queue::vecQueue::value_type
T value_type
类型别名定义
Definition: vecQueue.hh:67
Queue::vecQueue::front
reference & front()
Returns a reference to the next element in the queue.
Definition: vecQueue.hh:235
Queue::vecQueue::~vecQueue
virtual ~vecQueue()
Destroy the vec Queue object.
Definition: vecQueue.hh:217
Queue::vecQueue::reference
value_type & reference
数据的引用
Definition: vecQueue.hh:68
Queue::vecQueue::const_reference
const typedef value_type & const_reference
数据的常量引用
Definition: vecQueue.hh:69
Queue.h
Queue::vecQueue::size_type
size_t size_type
计数器类型
Definition: vecQueue.hh:70
Queue::vecQueue::back
reference & back()
Returns a reference to the last element in the queue.
Definition: vecQueue.hh:247
Queue::vecQueue::pop
void pop()
Removes the next element in the queue.
Definition: vecQueue.hh:277
Queue::vecQueue::_data
value_type * _data
存储队列的动态数组
Definition: vecQueue.hh:82
Queue
自定义的队列类都在Queue名字空间下(linkQueue.hh)
Definition: linkQueue.hh:25
Queue::vecQueue
队头位置固定的队列类
Definition: vecQueue.hh:46
Queue::Queue::size_type
size_t size_type
计数器类型
Definition: Queue.h:69
Queue::Queue::reference
value_type & reference
数据的引用
Definition: Queue.h:67
Queue::vecQueue::_rear
size_type _rear
队尾下标
Definition: vecQueue.hh:90
Queue::vecQueue::empty
bool empty() const
Test whether container is empty.
Definition: vecQueue.hh:223
Queue::vecQueue::_maxSize
size_type _maxSize
内部数组的规模
Definition: vecQueue.hh:76
Queue::vecQueue::push
void push(const value_type &val)
Inserts a new element at the end of the queue, after its current last element.
Definition: vecQueue.hh:259
Queue::vecQueue::vecQueue
vecQueue(size_type initSize=10)
Construct a new vec Queue object.
Definition: vecQueue.hh:211