数据结构实验5
deque.h
Go to the documentation of this file.
1 
11 //
12 // Created by emon100 on 2019/12/9 0009.
13 #ifndef DEQUE_DEQUE_H
14 #define DEQUE_DEQUE_H
15 
16 #include <utility>
17 #include <exception>
18 
19 
22 namespace my {
23 
24  const long MAXSIZE=128;//<对队列最大长度的宏定义
25 
29  template<typename T>
30  class deque {
31  private:
32  typedef long size_type;
33 
34  struct Node;
35  class const_iterator;//<队列常迭代器
36  class iterator;//<队列迭代器
37 
38  size_type const m_max_size=my::MAXSIZE;//<队列最大长度
39  size_type m_size;//<队列当前长度
40 
41  Node *m_front_ptr;//<队列头指针
42  Node *m_tail_ptr;//<队列尾指针
43  protected:
44  public:
49  [[nodiscard]] size_type size() const noexcept{ return m_size;}
50 
51 
58  [[nodiscard]] size_type max_size() const noexcept{ return m_max_size;}
59 
60  [[nodiscard]] bool empty() const noexcept{ return m_size==0;};
61 
62  void push_front(const T &x){
63  if(size()>=max_size()){
64  return;
65  } else if (empty()) {
66  m_front_ptr= m_tail_ptr=new Node(x);
67  ++m_size;
68  } else {
69  m_front_ptr = m_front_ptr->next =new Node(x,m_front_ptr);
70  ++m_size;
71  }
72  }
73  void push_front(T &&x){/*TODO*/
74  if(size()>=max_size()){
75  return;
76  } else if (empty()) {
77  m_front_ptr= m_tail_ptr=new Node(x);
78  ++m_size;
79  } else {
80  m_front_ptr = m_front_ptr->next =new Node(std::move(x),m_front_ptr);
81  ++m_size;
82  }
83  }
84  void push_back(const T &x) {
85  if (size() >= max_size()) {
86  return;
87  } else if (empty()) {
88  m_front_ptr = m_tail_ptr = new Node(x);
89  ++m_size;
90  } else {
91  m_tail_ptr = m_tail_ptr->prev = new Node(x, nullptr ,m_tail_ptr);
92  ++m_size;
93  }
94  }
95  void push_back(T &&x){
96  if (size() >= max_size()) {
97  return;
98  } else if (empty()) {
99  m_front_ptr = m_tail_ptr = new Node(x);
100  ++m_size;
101  } else {
102  m_tail_ptr = m_tail_ptr->prev = new Node(x, nullptr ,m_tail_ptr);
103  ++m_size;
104  }
105  }
106  void pop_front(){
107  if(m_size==0)
108  return;
109  else if (m_size==1){
110  delete m_front_ptr;
111  m_front_ptr=m_tail_ptr=nullptr;
112  }else {
113  m_front_ptr = m_front_ptr->prev;
114  delete m_front_ptr->next;
115  }
116  m_size--;
117  }
118  void pop_back(){
119  if(m_size==0)
120  return;
121  else if(m_size==1){
122  delete m_tail_ptr;
123  m_front_ptr=m_tail_ptr=nullptr;
124  } else{
125  m_tail_ptr=m_tail_ptr->next;
126  delete m_tail_ptr->prev;
127  }
128  m_size--;
129  }
130 
131  T &front(){ return m_front_ptr->data;}
132  const T &front() const{ return m_front_ptr->data;}
133 
134  T &back(){ return m_tail_ptr->data;}
135  const T &back() const { return m_tail_ptr->data;}
136 
137  const_iterator begin()const noexcept {
138  return const_iterator(m_tail_ptr);
139  }
140  const_iterator end()const noexcept {
141  return const_iterator();
142  }
143 
144  iterator begin() noexcept {
145  return iterator(m_tail_ptr);
146  }
147  iterator end() noexcept {
148  return iterator();
149  }
150 
151  T &at(size_type pos){
152  if(pos >= m_size){
153  throw std::out_of_range("Out of range");
154  }else{
155  return (*this)[pos];
156  }
157  }
158 
159  const T &at(size_type pos) const{
160  if(pos >= m_size){
161  throw std::out_of_range("Out of range");
162  }else{
163  return (*this)[pos];
164  }
165  }
166 
167  T &operator[](size_type pos)noexcept {
168  iterator a=this->begin();
169  for (int i = 0; i < pos ; ++i,++a);
170  return *a;
171  }
172 
173  const T &operator[](size_type pos) const noexcept{
174  const iterator a=this->begin();
175  for (int i = 0; i < pos ; ++i,++a);
176  return *a;
177  }
178 
179 
181  deque():
182  m_size(0),
183  m_front_ptr(nullptr),
184  m_tail_ptr(nullptr) {}
185 
188  deque(const deque &x) :
189  m_size(0),
190  m_tail_ptr(nullptr),
191  m_front_ptr(nullptr){
192  for(auto a:x){
193  push_front(a);
194  }
195  }
196 
199  deque &operator=(const deque &x){
200  if(this!=&x) {
201  deque copy = x;
202  std::swap(*this, copy);
203  }
204  return *this;
205  }
206 
211  deque(deque &&x) noexcept:
212  m_tail_ptr(x.m_tail_ptr),
213  m_front_ptr(x.m_front_ptr),
214  m_size(x.m_size),
215  m_max_size(x.m_max_size)
216  {
217  x.m_size=0;
218  x.m_tail_ptr=x.m_front_ptr=nullptr;
219  }
220 
225  deque &operator=(deque &&x)noexcept {
226  if(this != &x) {
227  std::swap(m_size, x.m_size);
228  std::swap(m_front_ptr, x.m_front_ptr);
229  std::swap(m_tail_ptr, x.m_tail_ptr);
230  }
231  return *this;
232  }
233 
239  deque(std::initializer_list<T> x) :
240  m_size(0),
241  m_max_size(128),
242  m_tail_ptr(nullptr),
243  m_front_ptr(nullptr){
244  for (auto a:x) {
245  push_front(a);
246  }
247  }
248 
254  deque &operator=(std::initializer_list<T> x){
255  *this = deque<T>(x);
256  return *this;
257  }
258 
261  while(m_size) {
262  pop_back();
263  }
264  }
265  };
266 
267  template <typename T>
268  struct deque<T>::Node {
269  T data;
272 
273  explicit Node(const T &d=T{},Node *p= nullptr,Node *n= nullptr):
274  data(d),
275  prev(p),
276  next(n){}
277 
278  explicit Node(T &&d,Node *p = nullptr, Node *n = nullptr):
279  data(std::move(d)),
280  prev(p),
281  next(n){}
282  };
283 
284  template <typename T>
285  class deque<T>::const_iterator {
286  public:
287  const_iterator():current(nullptr){}
288  const T &operator*()const{ return retrieve();}
290  current = current->next;
291  return *this;
292  }
293 
295  const_iterator old =*this;
296  ++(*this);
297  return old;
298  }
299 
300  bool operator==(const const_iterator &rhs)const{ return current == rhs.current;}
301  bool operator!=(const const_iterator &rhs)const{ return !(current == rhs.current);}
302 
303  protected:
304  T & retrieve() const{ return current->data; }
306  const_iterator(Node *p):current(p){}
307  friend class deque<T>;
308  };
309 
310  template <typename T>
311  class deque<T>::iterator : public deque<T>::const_iterator{
312  public:
314  T &operator*(){
315  return const_iterator::retrieve();
316  }
318  this->current=this->current->next;
319  return *this;
320  }
322  iterator old=*this;
323  ++(*this);
324  return old;
325  }
326 
327  protected:
329 
330  friend class deque<T>;
331  };
332 
333 }
334 #endif //DEQUE_DEQUE_H
my::deque
Definition: deque.h:30
my
my::deque::Node::data
T data
Definition: deque.h:269
my::MAXSIZE
const long MAXSIZE
Definition: deque.h:24
my::deque::begin
iterator begin() noexcept
Definition: deque.h:144
my::deque::size
size_type size() const noexcept
Definition: deque.h:49
my::deque::max_size
size_type max_size() const noexcept
Definition: deque.h:58
my::deque::const_iterator::operator!=
bool operator!=(const const_iterator &rhs) const
Definition: deque.h:301
my::deque::iterator::iterator
iterator()
Definition: deque.h:313
my::deque::iterator::operator*
T & operator*()
Definition: deque.h:314
my::deque::end
const_iterator end() const noexcept
Definition: deque.h:140
my::deque::Node::Node
Node(const T &d=T{}, Node *p=nullptr, Node *n=nullptr)
Definition: deque.h:273
my::deque::operator=
deque & operator=(deque &&x) noexcept
Definition: deque.h:225
my::deque::front
T & front()
Definition: deque.h:131
my::deque::deque
deque(deque &&x) noexcept
Definition: deque.h:211
my::deque::const_iterator::current
Node * current
Definition: deque.h:305
my::deque::push_front
void push_front(const T &x)
Definition: deque.h:62
my::deque::push_back
void push_back(const T &x)
Definition: deque.h:84
my::deque::Node::Node
Node(T &&d, Node *p=nullptr, Node *n=nullptr)
Definition: deque.h:278
my::deque::const_iterator::operator++
const_iterator & operator++()
Definition: deque.h:289
my::deque::const_iterator::operator==
bool operator==(const const_iterator &rhs) const
Definition: deque.h:300
my::deque::const_iterator::retrieve
T & retrieve() const
Definition: deque.h:304
my::deque::operator=
deque & operator=(const deque &x)
Definition: deque.h:199
my::deque::end
iterator end() noexcept
Definition: deque.h:147
my::deque::push_front
void push_front(T &&x)
Definition: deque.h:73
my::deque::deque
deque(std::initializer_list< T > x)
Definition: deque.h:239
my::deque::iterator
Definition: deque.h:311
my::deque::pop_back
void pop_back()
Definition: deque.h:118
my::deque::push_back
void push_back(T &&x)
Definition: deque.h:95
deque< T >
my::deque::at
const T & at(size_type pos) const
Definition: deque.h:159
my::deque::iterator::operator++
iterator operator++(int)
Definition: deque.h:321
my::deque::iterator::operator++
iterator & operator++()
Definition: deque.h:317
my::deque::empty
bool empty() const noexcept
Definition: deque.h:60
my::deque::const_iterator::const_iterator
const_iterator(Node *p)
Definition: deque.h:306
my::deque::pop_front
void pop_front()
Definition: deque.h:106
my::deque::const_iterator
Definition: deque.h:285
my::deque::~deque
~deque()
Definition: deque.h:260
my::deque::const_iterator::operator++
const_iterator operator++(int)
Definition: deque.h:294
my::deque::back
const T & back() const
Definition: deque.h:135
my::deque::front
const T & front() const
Definition: deque.h:132
my::deque::operator=
deque & operator=(std::initializer_list< T > x)
Definition: deque.h:254
my::deque::const_iterator::operator*
const T & operator*() const
Definition: deque.h:288
my::deque::Node::next
Node * next
Definition: deque.h:271
my::deque::deque
deque(const deque &x)
Definition: deque.h:188
my::deque::Node::prev
Node * prev
Definition: deque.h:270
my::deque::Node
Definition: deque.h:268
my::deque::at
T & at(size_type pos)
Definition: deque.h:151
my::deque::deque
deque()
Definition: deque.h:181
my::deque::iterator::iterator
iterator(Node *p)
Definition: deque.h:328
my::deque::operator[]
T & operator[](size_type pos) noexcept
Definition: deque.h:167
my::deque::back
T & back()
Definition: deque.h:134
my::deque::operator[]
const T & operator[](size_type pos) const noexcept
Definition: deque.h:173
my::deque::const_iterator::const_iterator
const_iterator()
Definition: deque.h:287
my::deque::begin
const_iterator begin() const noexcept
Definition: deque.h:137