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