队列  0.1
数据结构_第4章
linkQueue.hh
Go to the documentation of this file.
1 /****************************************************
2  * @file linkQueue.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 __LINKQUEUE_HH__
16 #define __LINKQUEUE_HH__
17 
18 #include "Queue.h"
19 #include <utility>
20 
25 namespace Queue
26 {
27 
33 template <typename T>
34 class linkQueue : 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:
53  struct node
54  {
60 
65  node *_next;
66 
73  node(const value_type &data = value_type(), node *next = nullptr)
74  : _data(data), _next(next) {}
75 
82  node(value_type &&data, node *next = nullptr)
83  : _data(std::move(data)), _next(next) {}
84 
89  ~node() = default;
90  };
91 
96  node *_front;
97 
102  node *_rear;
103 
109 
110 public:
115  linkQueue();
116 
121  virtual ~linkQueue();
122 
129  virtual bool isEmpty() const
130  {
131  return empty();
132  }
133 
139  virtual void enQueue(const_reference &x)
140  {
141  push(x);
142  }
143 
149  virtual value_type deQueue()
150  {
151  value_type retVal = front();
152  pop();
153  return retVal;
154  }
155 
161  virtual value_type getHead() const
162  {
163  return front();
164  }
165 
172  bool empty() const;
173 
179  size_type size() const;
180 
186  reference &front();
187 
193  const_reference &front() const;
194 
200  reference &back();
201 
207  const_reference &back() const;
208 
215  void push(const value_type &val);
216 
223  void push(value_type &&val);
224 
230  void pop();
231 };
232 
233 template <class T>
235  : _front(nullptr), _rear(nullptr), _size(0)
236 {
237 }
238 
239 template <class T>
241 {
242  node *delPtr = _front;
243  while (_front)
244  {
245  delPtr = _front;
246  _front = _front->_next;
247  delete delPtr;
248  }
249 }
250 
251 template <class T>
253 {
254  return _front == nullptr;
255 }
256 
257 template <class T>
259 {
260  return _size;
261 }
262 
263 template <class T>
265 {
266  return _front->_data;
267 }
268 
269 template <class T>
271 {
272  return _front->_data;
273 }
274 
275 template <class T>
277 {
278  return _rear->_data;
279 }
280 
281 template <class T>
283 {
284  return _rear->_data;
285 }
286 
287 template <class T>
289 {
290  // ն if (!_front) _rear = _front = new node(val); else _rear = _rear->_next = new node(val); ++_size; } template <class T> void linkQueue<T>::push(value_type &&val) { // ն if (!_front) _rear = _front = new node(std::move(val)); else _rear = _rear->_next = new node(std::move(val)); ++_size; } template <class T> void linkQueue<T>::pop() { node *delPtr = _front; _front = _front->_next; delete delPtr; --_size; // һԪس if (!_front) _rear = nullptr; } } // namespace Queue #endif // __LINKQUEUE_HH__
291  if (!_front)
292  _rear = _front = new node(val);
293  else
294  _rear = _rear->_next = new node(val);
295  ++_size;
296 }
297 
298 template <class T>
299 void linkQueue<T>::push(value_type &&val)
300 {
301  // ն if (!_front) _rear = _front = new node(std::move(val)); else _rear = _rear->_next = new node(std::move(val)); ++_size; } template <class T> void linkQueue<T>::pop() { node *delPtr = _front; _front = _front->_next; delete delPtr; --_size; // һԪس if (!_front) _rear = nullptr; } } // namespace Queue #endif // __LINKQUEUE_HH__
302  if (!_front)
303  _rear = _front = new node(std::move(val));
304  else
305  _rear = _rear->_next = new node(std::move(val));
306  ++_size;
307 }
308 
309 template <class T>
310 void linkQueue<T>::pop()
311 {
312  node *delPtr = _front;
313  _front = _front->_next;
314  delete delPtr;
315  --_size;
316 
317  // һԪس if (!_front) _rear = nullptr; } } // namespace Queue #endif // __LINKQUEUE_HH__
318  if (!_front)
319  _rear = nullptr;
320 }
321 
322 } // namespace Queue
323 
324 #endif // __LINKQUEUE_HH__
Queue::linkQueue::node::~node
~node()=default
Destroy the node object.
Queue::linkQueue::back
reference & back()
Returns a reference to the last element in the queue.
Definition: linkQueue.hh:288
Queue.h
Queue::linkQueue::node
linkQueueĽ
Definition: linkQueue.hh:77
Queue::linkQueue::const_reference
const typedef value_type & const_reference
ݵij
Definition: linkQueue.hh:69
Queue::linkQueue::getHead
virtual value_type getHead() const
Get the Head object.
Definition: linkQueue.hh:185
Queue::linkQueue::_rear
node * _rear
ָβ
Definition: linkQueue.hh:126
Queue::linkQueue::value_type
T value_type
ͱ
Definition: linkQueue.hh:67
Queue::linkQueue::linkQueue
linkQueue()
Construct a new link Queue object.
Definition: linkQueue.hh:246
Queue::linkQueue
Ӷ
Definition: linkQueue.hh:46
Queue::linkQueue::front
reference & front()
Returns a reference to the next element in the queue.
Definition: linkQueue.hh:276
Queue::linkQueue::reference
value_type & reference
ݵ
Definition: linkQueue.hh:68
Queue::linkQueue::_size
size_type _size
еǰԪظ
Definition: linkQueue.hh:132
Queue::linkQueue::empty
bool empty() const
Test whether container is empty.
Definition: linkQueue.hh:264
Queue::linkQueue::size_type
size_t size_type
Definition: linkQueue.hh:70
Queue::linkQueue::_front
node * _front
ָ׽
Definition: linkQueue.hh:120
Queue::linkQueue::deQueue
virtual value_type deQueue()
һԪ
Definition: linkQueue.hh:173
Queue::linkQueue::isEmpty
virtual bool isEmpty() const
жӿ
Definition: linkQueue.hh:153
Queue::linkQueue::~linkQueue
virtual ~linkQueue()
Destroy the link Queue object.
Definition: linkQueue.hh:252
Queue
ԶĶ඼Queueֿռ(linkQueue.hh)
Definition: linkQueue.hh:25
Queue::linkQueue::push
void push(const value_type &val)
Inserts a new element at the end of the queue, after its current last element.
Definition: linkQueue.hh:300
Queue::linkQueue::enQueue
virtual void enQueue(const_reference &x)
һԪ
Definition: linkQueue.hh:163
Queue::linkQueue::node::_data
value_type _data
Definition: linkQueue.hh:83
Queue::linkQueue::pop
void pop()
Removes the next element in the queue.
Definition: linkQueue.hh:322
Queue::linkQueue::size
size_type size() const
Returns the number of elements in the queue.
Definition: linkQueue.hh:270
Queue::linkQueue::node::node
node(const value_type &data=value_type(), node *next=nullptr)
Construct a new node object.
Definition: linkQueue.hh:97
Queue::linkQueue::node::_next
node * _next
ָnodeָ
Definition: linkQueue.hh:89