队列  0.1
数据结构_第4章
seqQueue.hh
Go to the documentation of this file.
1 /****************************************************
2  * @file seqQueue.hh
3  * @author Guorui Wei (313017602@qq.com)
4  * @brief 循环队列的定义和实现
5  * @version 0.1
6  * @date 2020-04-05
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 __SEQQUEUE_HH__
16 #define __SEQQUEUE_HH__
17 
18 #include "Queue.h"
19 #include <utility>
20 
25 namespace Queue
26 {
27 
33 template <typename T>
34 class seqQueue : 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 
48 private:
54 
62 
72 
77  void doubleSpace();
78 
79 public:
85  seqQueue(size_type initSize = 10);
86 
91  virtual ~seqQueue();
92 
99  virtual bool isEmpty() const
100  {
101  return empty();
102  }
103 
109  virtual void enQueue(const_reference &x)
110  {
111  push(x);
112  }
113 
119  virtual value_type deQueue()
120  {
121  value_type retVal = front();
122  pop();
123  return retVal;
124  }
125 
131  virtual value_type getHead() const
132  {
133  return front();
134  }
135 
142  bool empty() const;
143 
149  size_type size() const;
150 
156  reference &front();
157 
163  const_reference &front() const;
164 
170  reference &back();
171 
177  const_reference &back() const;
178 
185  void push(const value_type &val);
186 
193  void push(value_type &&val);
194 
200  void pop();
201 };
202 
203 template <class T>
204 seqQueue<T>::seqQueue(size_type initSize)
205  : _elem(new value_type[initSize]), _maxSize(initSize), _front(0), _rear(0)
206 {
207 }
208 
209 template <class T>
211 {
212  delete[] _elem;
213 }
214 
215 template <class T>
216 bool seqQueue<T>::empty() const
217 {
218  return _front == _rear;
219 }
220 
221 template <class T>
223 {
224  return ((_rear >= _front) ? _rear - _front : _maxSize + _rear - _front);
225 }
226 
227 template <class T>
229 {
230  return _elem[(_front + 1) % _maxSize];
231 }
232 
233 template <class T>
235 {
236  return _elem[(_front + 1) % _maxSize];
237 }
238 
239 template <class T>
241 {
242  return _elem[_rear];
243 }
244 
245 template <class T>
247 {
248  return _elem[_rear];
249 }
250 
251 template <class T>
253 {
254  if (size() == _maxSize - 1)
255  doubleSpace();
256 
257  _elem[_rear = (_rear + 1) % _maxSize] = val;
258 }
259 
260 template <class T>
261 void seqQueue<T>::push(value_type &&val)
262 {
263  if (size() == _maxSize - 1)
264  doubleSpace();
265 
266  _elem[_rear = (_rear + 1) % _maxSize] = std::move(val);
267 }
268 
269 template <class T>
270 void seqQueue<T>::pop()
271 {
272  _front = (_front + 1) % _maxSize;
273 }
274 
275 template <class T>
277 {
278  value_type *old = _elem;
279  _elem = new value_type[2 * _maxSize + 1];
280 
281  for (size_type i = 1; i <= size(); ++i)
282  _elem[i] = old[(_front + i) % _maxSize];
283  delete[] old;
284 
285  _front = 0;
286  _rear = size();
287  _maxSize = 2 * _maxSize + 1;
288 }
289 
290 } // namespace Queue
291 
292 #endif
Queue.h
Queue::seqQueue::size_type
size_t size_type
计数器类型
Definition: seqQueue.hh:70
Queue::seqQueue
循环队列类
Definition: seqQueue.hh:46
Queue::seqQueue::~seqQueue
virtual ~seqQueue()
Destroy the seq Queue object.
Definition: seqQueue.hh:222
Queue::seqQueue::doubleSpace
void doubleSpace()
扩大数组空间
Definition: seqQueue.hh:288
Queue::seqQueue::value_type
T value_type
类型别名定义
Definition: seqQueue.hh:67
Queue::seqQueue::_rear
size_type _rear
Definition: seqQueue.hh:95
Queue::seqQueue::push
void push(const value_type &val)
Inserts a new element at the end of the queue, after its current last element.
Definition: seqQueue.hh:264
Queue::seqQueue::empty
bool empty() const
Test whether container is empty.
Definition: seqQueue.hh:228
Queue::seqQueue::isEmpty
virtual bool isEmpty() const
判队空
Definition: seqQueue.hh:123
Queue::seqQueue::back
reference & back()
Returns a reference to the last element in the queue.
Definition: seqQueue.hh:252
Queue::seqQueue::_maxSize
size_type _maxSize
数组的容量
Definition: seqQueue.hh:85
Queue::seqQueue::getHead
virtual value_type getHead() const
Get the Head object.
Definition: seqQueue.hh:155
Queue
ԶĶ඼Queueֿռ(linkQueue.hh)
Definition: linkQueue.hh:25
Queue::Queue::size_type
size_t size_type
计数器类型
Definition: Queue.h:69
Queue::seqQueue::seqQueue
seqQueue(size_type initSize=10)
Construct a new seq Queue object.
Definition: seqQueue.hh:216
Queue::seqQueue::_elem
value_type * _elem
存储元素的内部数组
Definition: seqQueue.hh:77
Queue::seqQueue::pop
void pop()
Removes the next element in the queue, effectively reducing its size by one.
Definition: seqQueue.hh:282
Queue::seqQueue::front
reference & front()
Returns a reference to the next element in the queue.
Definition: seqQueue.hh:240
Queue::Queue::reference
value_type & reference
数据的引用
Definition: Queue.h:67
Queue::seqQueue::deQueue
virtual value_type deQueue()
出队一个元素
Definition: seqQueue.hh:143
Queue::seqQueue::enQueue
virtual void enQueue(const_reference &x)
入队一个元素
Definition: seqQueue.hh:133
Queue::seqQueue::const_reference
const typedef value_type & const_reference
数据的常量引用
Definition: seqQueue.hh:69
Queue::seqQueue::size
size_type size() const
Returns the number of elements in the queue.
Definition: seqQueue.hh:234
Queue::seqQueue::_front
size_type _front
队头/尾下标 数据范围(front, rear]: 在循环意义下 初始状态: front == rear == 0 队列满: front == (rear + 1) % maxSize 队列空: fro...
Definition: seqQueue.hh:95
Queue::seqQueue::reference
value_type & reference
数据的引用
Definition: seqQueue.hh:68