优先级队列  0.1
数据结构_第7章
dLinkList.h
Go to the documentation of this file.
1 
14 #ifndef __dLinkListIncluded
15 #define __dLinkListIncluded
16 
17 #include "List.h"
18 #include <iostream>
19 #include <utility>
20 
21 namespace List
22 {
23  // 模板使用前须先声明
24  template <class T>
25  class dLinkList;
26 
27  template <class T>
28  dLinkList<T> operator+(const dLinkList<T> &A, const dLinkList<T> &B); // 将B拼接到A后
29 
30  // 双链表类的定义
31  template <typename T>
32  class dLinkList : public List<T>
33  {
34  private:
35  struct node
36  {
37  T data;
39 
40  node(const T &_obj = T(), node *_prev = nullptr, node *_next = nullptr)
41  : data(_obj), prev(_prev), next(_next) {}
42  node(T &&_obj, node *_prev = nullptr, node *_next = nullptr)
43  : data(std::move(_obj)), prev(_prev), next(_next) {}
44 
45  ~node() = default;
46  };
47 
48  node *head, *tail; // 头尾指针
49  int currentLength; // 表长
50  node *move(int i) const; // 返回指向第i个结点的指针,i = -1表示头指针
51 
52  public:
54  {
55  public:
56  const_iterator() : current(nullptr) {}
57  const T &operator*() const { return current->data; }
59  {
60  current = current->next;
61  return *this;
62  }
64  {
65  const_iterator old = *this;
66  current = current->next;
67  return old;
68  }
70  {
71  current = current->prev;
72  return *this;
73  }
75  {
76  const_iterator old = *this;
77  current = current->prev;
78  return old;
79  }
80  bool operator==(const const_iterator &rhs) const { return rhs.current == current; }
81  bool operator!=(const const_iterator &rhs) const { return rhs.current != current; }
82 
83  protected:
86 
87  friend dLinkList<T>;
88  };
89 
90  class iterator : public const_iterator
91  {
92  public:
93  T &operator*() { return iterator::current->data; }
94  const T &operator*() const { return const_iterator::operator*(); } // 与基类完全相同。但仍要显式定义,否则会被派生类定义的mutator同名函数redefined,导致基类同名函数不可见
96  {
98  return *this;
99  }
101  {
102  iterator old = *this;
104  return old;
105  }
106  // 与基类相同
107  // virtual bool operator==(const iterator &rhs) const;
108  // virtual bool operator!=(const iterator &rhs) const;
109  protected:
111 
112  friend dLinkList<T>;
113  };
114 
115  public:
116  dLinkList();
117  dLinkList(const dLinkList<T> &rhs);
118  dLinkList(dLinkList<T> &&rvalue); // 移动构造
119  dLinkList<T> &operator=(dLinkList<T> &&rvalue); //移动赋值
121  {
122  clear();
123  if (head)
124  delete head;
125  if (tail)
126  delete tail;
127  }
128 
129  virtual void clear(); // 删除线性表中所有的元素
130  virtual int length() const; // 返回线性表的长度
131  virtual void insert(int i, const T &obj); // 在第i个位置插入一个元素,即新元素的下标将为i
132  virtual void remove(int i); // 删除第i个位置的元素
133  virtual int search(const T &obj) const; // 返回元素obj在线性表中首次出现的下标
134  virtual T visit(int i) const; // 返回线性表中第i个元素
135  virtual void traverse() const = delete; // 遍历线性表
136  friend dLinkList<T> operator+<T>(const dLinkList<T> &A, const dLinkList<T> &B); // 将B拼接到A后
137 
138  virtual iterator begin() { return iterator(head->next); } // 数据范围[begin, end)
139  virtual const_iterator begin() const { return iterator(head->next); } //
140  void push_back(const T &obj); //
141  void push_back(T &&obj); // 向表尾添加一个新元素
142  void pop_back(); // Delete last element
143  const T &back() const { return tail->prev->data; } // Returns a reference to the last element.
144  T &back() { return tail->prev->data; } // Returns a reference to the last element in the list container.
145 
146  // 是否允许delete基类成员函数?
147  virtual iterator end() { return iterator(tail); }
148  virtual const_iterator end() const { return iterator(tail); }
149  };
150 
151  // 友元函数模板的定义
152  template <class T>
153  dLinkList<T> operator+(const dLinkList<T> &A, const dLinkList<T> &B) // 将B拼接到A后
154  {
155  dLinkList<T> C;
156 
157  typename dLinkList<T>::node *p = A.head->next;
158  while (p != A.tail)
159  {
160  C.push_back(p->data);
161  p = p->next;
162  }
163 
164  p = B.head->next;
165  while (p != B.tail)
166  {
167  C.push_back(p->data);
168  p = p->next;
169  }
170 
171  return C;
172  }
173 
174  // 双链表类的实现
175  template <class T>
177  : head(new node()), tail(new node()), currentLength(0)
178  {
179  head->next = tail;
180  tail->prev = head;
181  }
182 
183  template <class T>
185  : head(rvalue.head), tail(rvalue.tail), currentLength(rvalue.currentLength)
186  {
187  rvalue.tail = rvalue.head = nullptr;
188  }
189 
190  template <class T>
192  : dLinkList()
193  {
194  for (const_iterator i = rhs.begin(); i != rhs.end(); ++i)
195  push_back(*i);
196  }
197 
198  template <class T>
200  {
201  std::swap(head, rvalue.head);
202  std::swap(tail, rvalue.tail);
203  std::swap(currentLength, rvalue.currentLength);
204  return *this;
205  }
206 
207  template <class T>
209  {
210  return currentLength;
211  }
212 
213  template <class T>
214  typename dLinkList<T>::node *dLinkList<T>::move(int i) const
215  {
216  node *ptr = head;
217  for (int k = 0; k <= i; ++k)
218  ptr = ptr->next;
219 
220  return ptr;
221  }
222 
223  template <class T>
224  void dLinkList<T>::insert(int i, const T &obj)
225  {
226  node *pos{move(i)};
227 
228  pos->prev = pos->prev->next = new node(obj, pos->prev, pos);
229 
230  ++currentLength;
231  }
232 
233  template <class T>
235  {
236  node *delp = move(i);
237 
238  delp->prev->next = delp->next;
239  delp->next->prev = delp->prev;
240  delete delp;
241 
242  --currentLength;
243  }
244 
245  template <class T>
247  {
248  if (!head || !tail) // 移动构造前,右值的head和tail将被置0
249  return;
250 
251  node *p = head->next, *q;
252 
253  head->next = tail;
254  tail->prev = head;
255 
256  while (p != tail)
257  {
258  q = p->next;
259  delete p;
260  p = q;
261  }
262 
263  currentLength = 0;
264  }
265 
266  template <class T>
267  int dLinkList<T>::search(const T &obj) const
268  {
269  node *p = head->next;
270  int i = 0;
271 
272  while (p != tail)
273  {
274  if (p->data == obj)
275  return i;
276 
277  p = p->next;
278  ++i;
279  }
280 
281  return -1;
282  }
283 
284  template <class T>
285  T dLinkList<T>::visit(int i) const
286  {
287  return move(i)->data;
288  }
289 
290  // template <class T>
291  // void dLinkList<T>::traverse() const
292  // {
293  // std::cout << '\n';
294 
295  // node *p = head->next;
296  // while (p != tail)
297  // {
298  // std::cout << p->data << ' ';
299  // p = p->next;
300  // }
301 
302  // std::cout << std::endl;
303  // }
304 
305  template <class T>
306  void dLinkList<T>::push_back(const T &obj) // 向表尾添加一个新元素
307  {
308  tail->prev = tail->prev->next = new node(obj, tail->prev, tail);
309  }
310 
311  template <class T>
312  void dLinkList<T>::push_back(T &&obj) // 向表尾添加一个新元素
313  {
314  tail->prev = tail->prev->next = new node(std::move(obj), tail->prev, tail);
315  }
316 
317  // Delete last element
318  template <class T>
320  {
321  node *delPtr = tail->prev;
322  delPtr->prev->next = tail;
323  tail->prev = delPtr->prev;
324  delete delPtr;
325  }
326 
327 } // namespace List
328 #endif
List::dLinkList::const_iterator::current
node * current
Definition: dLinkList.h:84
List::dLinkList::begin
virtual const_iterator begin() const
Definition: dLinkList.h:139
List::dLinkList::iterator::operator++
iterator operator++(int)
Definition: dLinkList.h:100
List::dLinkList::const_iterator::operator==
bool operator==(const const_iterator &rhs) const
Definition: dLinkList.h:80
List::dLinkList::node::data
T data
Definition: dLinkList.h:37
List::dLinkList::move
node * move(int i) const
Definition: dLinkList.h:214
List::dLinkList::end
virtual const_iterator end() const
Definition: dLinkList.h:148
List::dLinkList::node::next
node * next
Definition: dLinkList.h:38
List::dLinkList::const_iterator::const_iterator
const_iterator(node *p)
Definition: dLinkList.h:85
List::dLinkList::iterator::iterator
iterator(node *p)
Definition: dLinkList.h:110
List
Definition: dLinkList.h:21
List::dLinkList::iterator
Definition: dLinkList.h:90
List::dLinkList::visit
virtual T visit(int i) const
Definition: dLinkList.h:285
List::dLinkList::push_back
void push_back(const T &obj)
Definition: dLinkList.h:306
List::dLinkList::tail
node * tail
Definition: dLinkList.h:48
List::dLinkList::iterator::operator*
T & operator*()
Definition: dLinkList.h:93
List::dLinkList::length
virtual int length() const
Definition: dLinkList.h:208
List::dLinkList::const_iterator::const_iterator
const_iterator()
Definition: dLinkList.h:56
List::dLinkList::operator=
dLinkList< T > & operator=(dLinkList< T > &&rvalue)
Definition: dLinkList.h:199
List::dLinkList::~dLinkList
~dLinkList()
Definition: dLinkList.h:120
List::dLinkList::back
const T & back() const
Definition: dLinkList.h:143
List::dLinkList::const_iterator
Definition: dLinkList.h:53
List::dLinkList::search
virtual int search(const T &obj) const
Definition: dLinkList.h:267
List::dLinkList::pop_back
void pop_back()
Definition: dLinkList.h:319
List::operator+
dLinkList< T > operator+(const dLinkList< T > &A, const dLinkList< T > &B)
Definition: dLinkList.h:153
List::dLinkList::clear
virtual void clear()
Definition: dLinkList.h:246
List::dLinkList
Definition: dLinkList.h:25
List::dLinkList::iterator::operator++
iterator & operator++()
Definition: dLinkList.h:95
List::dLinkList::node::node
node(T &&_obj, node *_prev=nullptr, node *_next=nullptr)
Definition: dLinkList.h:42
List::dLinkList::traverse
virtual void traverse() const =delete
List::dLinkList::node
Definition: dLinkList.h:35
List.h
栈的抽象类
List::dLinkList::const_iterator::operator*
const T & operator*() const
Definition: dLinkList.h:57
List::dLinkList::const_iterator::operator--
const_iterator operator--(int)
Definition: dLinkList.h:74
List::dLinkList::iterator::operator*
const T & operator*() const
Definition: dLinkList.h:94
List::dLinkList::remove
virtual void remove(int i)
Definition: dLinkList.h:234
List::dLinkList::const_iterator::operator!=
bool operator!=(const const_iterator &rhs) const
Definition: dLinkList.h:81
List::dLinkList::dLinkList
dLinkList()
Definition: dLinkList.h:176
List::dLinkList::end
virtual iterator end()
Definition: dLinkList.h:147
List::dLinkList::node::prev
node * prev
Definition: dLinkList.h:38
List::dLinkList::begin
virtual iterator begin()
Definition: dLinkList.h:138
List::dLinkList::currentLength
int currentLength
Definition: dLinkList.h:49
List::dLinkList::node::~node
~node()=default
List::dLinkList::const_iterator::operator--
const_iterator & operator--()
Definition: dLinkList.h:69
List::dLinkList::node::node
node(const T &_obj=T(), node *_prev=nullptr, node *_next=nullptr)
Definition: dLinkList.h:40
List::dLinkList::const_iterator::operator++
const_iterator & operator++()
Definition: dLinkList.h:58
List::dLinkList::back
T & back()
Definition: dLinkList.h:144
List::dLinkList::insert
virtual void insert(int i, const T &obj)
Definition: dLinkList.h:224
List::dLinkList::head
node * head
Definition: dLinkList.h:48
List::dLinkList::const_iterator::operator++
const_iterator operator++(int)
Definition: dLinkList.h:63