树  0.1
数据结构_第6章
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 INCLUDE_SEQQUEUE_HH_
16 #define INCLUDE_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 
192  void push(value_type &&val);
193 
198  void pop();
199 };
200 
201 template <class T>
202 seqQueue<T>::seqQueue(size_type initSize)
203  : _elem(new value_type[initSize]), _maxSize(initSize), _front(0), _rear(0)
204 {
205 }
206 
207 template <class T>
209 {
210  delete[] _elem;
211 }
212 
213 template <class T>
214 bool seqQueue<T>::empty() const
215 {
216  return _front == _rear;
217 }
218 
219 template <class T>
221 {
222  return ((_rear >= _front) ? _rear - _front : _maxSize + _rear - _front);
223 }
224 
225 template <class T>
227 {
228  return _elem[(_front + 1) % _maxSize];
229 }
230 
231 template <class T>
233 {
234  return _elem[(_front + 1) % _maxSize];
235 }
236 
237 template <class T>
239 {
240  return _elem[_rear];
241 }
242 
243 template <class T>
245 {
246  return _elem[_rear];
247 }
248 
249 template <class T>
251 {
252  if (size() == _maxSize - 1)
253  doubleSpace();
254 
255  _elem[_rear = (_rear + 1) % _maxSize] = val;
256 }
257 
258 template <class T>
259 void seqQueue<T>::push(value_type &&val)
260 {
261  if (size() == _maxSize - 1)
262  doubleSpace();
263 
264  _elem[_rear = (_rear + 1) % _maxSize] = std::move(val);
265 }
266 
267 template <class T>
268 void seqQueue<T>::pop()
269 {
270  _front = (_front + 1) % _maxSize;
271 }
272 
273 template <class T>
275 {
276  value_type *old = _elem;
277  _elem = new value_type[2 * _maxSize + 1];
278 
279  for (size_type i = 1; i <= size(); ++i)
280  _elem[i] = old[(_front + i) % _maxSize];
281  delete[] old;
282 
283  _front = 0;
284  _rear = size();
285  _maxSize = 2 * _maxSize + 1;
286 }
287 
288 } // namespace Queue
289 
290 #endif /* INCLUDE_SEQQUEUE_HH_ */
Queue::seqQueue
循环队列类
Definition: seqQueue.hh:46
Queue::seqQueue::size_type
size_t size_type
计数器类型
Definition: seqQueue.hh:70
Queue::seqQueue::deQueue
virtual value_type deQueue()
出队一个元素
Definition: seqQueue.hh:143
Queue::seqQueue::getHead
virtual value_type getHead() const
Get the Head object.
Definition: seqQueue.hh:155
Queue::seqQueue::size
size_type size() const
Returns the number of elements in the queue.
Definition: seqQueue.hh:232
Queue::seqQueue::isEmpty
virtual bool isEmpty() const
判队空
Definition: seqQueue.hh:123
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:262
Queue::seqQueue::empty
bool empty() const
Test whether container is empty.
Definition: seqQueue.hh:226
Queue::seqQueue::const_reference
const typedef value_type & const_reference
数据的常量引用
Definition: seqQueue.hh:69
Queue::seqQueue::_elem
value_type * _elem
存储元素的内部数组
Definition: seqQueue.hh:77
Queue::seqQueue::doubleSpace
void doubleSpace()
扩大数组空间
Definition: seqQueue.hh:286
Queue::seqQueue::pop
void pop()
Removes the next element in the queue, effectively reducing its size by one.
Definition: seqQueue.hh:280
Queue::seqQueue::_front
size_type _front
队头/尾下标 数据范围(front, rear]: 在循环意义下 初始状态: front == rear == 0 队列满: front == (rear + 1) % maxSize 队列空: fro...
Definition: seqQueue.hh:95
Queue::seqQueue::front
reference & front()
Returns a reference to the next element in the queue.
Definition: seqQueue.hh:238
Queue.h
Queue::seqQueue::seqQueue
seqQueue(size_type initSize=10)
Construct a new seq Queue object.
Definition: seqQueue.hh:214
Queue
自定义的队列类都在Queue名字空间下(linkQueue.hh)
Definition: linkQueue.hh:25
Queue::seqQueue::_maxSize
size_type _maxSize
数组的容量
Definition: seqQueue.hh:85
Queue::seqQueue::reference
value_type & reference
数据的引用
Definition: seqQueue.hh:68
Queue::Queue::size_type
size_t size_type
计数器类型
Definition: Queue.h:69
Queue::Queue::reference
value_type & reference
数据的引用
Definition: Queue.h:67
Queue::seqQueue::enQueue
virtual void enQueue(const_reference &x)
入队一个元素
Definition: seqQueue.hh:133
Queue::seqQueue::_rear
size_type _rear
Definition: seqQueue.hh:95
Queue::seqQueue::~seqQueue
virtual ~seqQueue()
Destroy the seq Queue object.
Definition: seqQueue.hh:220
Queue::seqQueue::value_type
T value_type
类型别名定义
Definition: seqQueue.hh:67
Queue::seqQueue::back
reference & back()
Returns a reference to the last element in the queue.
Definition: seqQueue.hh:250