-
Notifications
You must be signed in to change notification settings - Fork 5
/
DoubleList.h
469 lines (415 loc) · 8.46 KB
/
DoubleList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Author : XuBenHao
// Version : 1.0.0
// Mail : xbh370970843@163.com
// Copyright : XuBenHao 2020 - 2030
#ifndef ALLIB_DATASTRUCT_LIST_DOUBLELIST_H
#define ALLIB_DATASTRUCT_LIST_DOUBLELIST_H
#include "..\..\stdafx.h"
namespace AlLib
{
namespace DataStruct
{
namespace List
{
template <typename T>
class DoubleList
{
public:
class Node
{
public:
T GetValue() const
{
return m_nValue;
}
void SetValue(const T& nValue_)
{
m_nValue = nValue_;
}
private:
Node()
{
m_pPre = nullptr;
m_pNext = nullptr;
}
~Node()
{
}
T m_nValue;
Node *m_pPre;
Node *m_pNext;
friend class DoubleList < T >;
};
DoubleList();
// 拷贝构造
DoubleList(const DoubleList& list_);
// 赋值
DoubleList<T>& operator=(const DoubleList& list_);
virtual ~DoubleList();
// 判空
bool IsEmpty() const;
// 容量
int GetSize() const;
// 插入:
// 插入元素成为首元素
void InsertFirst(const T& nNewValue_);
// 插入元素成为尾元素
void InsertLast(const T& nNewValue_);
// 插入元素成为尾元素
void Add(const T& nNewValue_);
// 在指定元素前面插入
void InsertBefore(Node* pElement_, const T& nNewValue_);
// 在指定元素后面插入
void InsertAfter(Node* pElement_, const T& nNewValue_);
// 删除
// 删除首元素
void DeleteFirst();
// 删除尾元素
void DeleteLast();
// 删除指定位置元素
void Delete(Node* pElement_);
// 删除指定值的首个元素
void Delete(const T& nValue_);
// 元素访问:
// 获取首个元素
Node* GetFirst() const;
// 获取尾部元素
Node* GetLast() const;
// 搜索
Node* Find(std::function<bool(const T&)> predictFunction_) const;
// 容器遍历:
// 获取输入元素下一元素
Node* GetNext(Node* pElement_) const;
// 获取输入元素上一元素
Node* GetPre(Node* pElement_) const;
private:
Node *m_pFirst;
Node *m_pLast;
};
template<typename T>
DoubleList<T>::DoubleList() :
m_pFirst(nullptr), m_pLast(nullptr)
{
}
template<typename T>
DoubleList<T>::~DoubleList()
{
// 保证:
// 非空时,尾元素下一元素指针为空。
// 为空时,m_pBegin,m_pEnd均为null。
Node *_pCur = m_pFirst;
while (_pCur)
{
Node* _pTemp = _pCur;
_pCur = _pCur->m_pNext;
delete _pTemp;
_pTemp = nullptr;
}
m_pFirst = nullptr;
m_pLast = nullptr;
}
template<typename T>
DoubleList<T>::DoubleList(const DoubleList& list_) :
m_pFirst(nullptr), m_pLast(nullptr)
{
Node *_pCur = list_.m_pFirst;
while (_pCur)
{
Node* _pTemp = _pCur;
_pCur = _pCur->m_pNext;
Add(_pTemp->GetValue());
}
}
template<typename T>
typename DoubleList<T>& DoubleList<T>::operator=(const DoubleList& list_)
{
if (this == &list_)
{
return *this;
}
this->~DoubleList();
// 假定:
Node *_pCur = list_.m_pFirst;
while (_pCur)
{
Node* _pTemp = _pCur;
_pCur = _pCur->m_pNext;
Add(_pTemp->GetValue());
}
return *this;
}
template<typename T>
bool DoubleList<T>::IsEmpty() const
{
if (m_pFirst == nullptr)
{
assert(m_pLast == nullptr);
return true;
}
else
{
return false;
}
}
template<typename T>
int DoubleList<T>::GetSize() const
{
Node *_pCur = m_pFirst;
int _nSize = 0;
while (_pCur)
{
_nSize++;
_pCur = _pCur->m_pNext;
}
return _nSize;
}
template<typename T>
void DoubleList<T>::InsertFirst(const T& nNewValue_)
{
Node *_pElement = nullptr;
try
{
_pElement = new Node();
}
catch (...)
{
_pElement = nullptr;
throw "out of memory";
}
_pElement->SetValue(nNewValue_);
if (IsEmpty() == true)
{
m_pFirst = _pElement;
m_pLast = _pElement;
}
else
{
_pElement->m_pNext = m_pFirst;
m_pFirst->m_pPre = _pElement;
m_pFirst = _pElement;
}
}
template<typename T>
void DoubleList<T>::InsertLast(const T& nNewValue_)
{
Node *_pElement = nullptr;
try
{
_pElement = new Node();
}
catch (...)
{
_pElement = nullptr;
throw "out of memory";
}
_pElement->SetValue(nNewValue_);
if (IsEmpty() == true)
{
m_pFirst = _pElement;
m_pLast = _pElement;
}
else
{
m_pLast->m_pNext = _pElement;
_pElement->m_pPre = m_pLast;
m_pLast = _pElement;
}
}
template<typename T>
void DoubleList<T>::Add(const T& nNewValue_)
{
InsertLast(nNewValue_);
}
template<typename T>
void DoubleList<T>::InsertBefore(Node* pElement_, const T& nNewValue_)
{
if (pElement_ == nullptr)
{
return;
}
Node* _pPre = pElement_->m_pPre;
if (_pPre == nullptr)
{
InsertFirst(nNewValue_);
}
else
{
Node *_pNewElement = nullptr;
try
{
_pNewElement = new Node();
}
catch (...)
{
_pNewElement = nullptr;
throw "out of memory";
}
_pNewElement->SetValue(nNewValue_);
_pPre->m_pNext = _pNewElement;
_pNewElement->m_pPre = _pPre;
pElement_->m_pPre = _pNewElement;
_pNewElement->m_pNext = pElement_;
}
}
template<typename T>
void DoubleList<T>::InsertAfter(Node* pElement_, const T& nNewValue_)
{
if (pElement_ == nullptr)
{
return;
}
Node* _pNext = pElement_->m_pNext;
if (_pNext == nullptr)
{
InsertLast(nNewValue_);
}
else
{
Node *_pNewElement = nullptr;
try
{
_pNewElement = new Node();
}
catch (...)
{
_pNewElement = nullptr;
throw "out of memory";
}
_pNewElement->SetValue(nNewValue_);
pElement_->m_pNext = _pNewElement;
_pNewElement->m_pPre = pElement_;
_pNext->m_pPre = _pNewElement;
_pNewElement->m_pNext = _pNext;
}
}
template<typename T>
void DoubleList<T>::DeleteFirst()
{
if (IsEmpty())
{
return;
}
else if (m_pFirst == m_pLast && m_pFirst != nullptr)
{
delete m_pFirst;
m_pFirst = nullptr;
m_pLast = nullptr;
}
else
{
Node* _pDelete = m_pFirst;
m_pFirst = _pDelete->m_pNext;
m_pFirst->m_pPre = nullptr;
delete _pDelete;
_pDelete = nullptr;
}
}
template<typename T>
void DoubleList<T>::DeleteLast()
{
if (IsEmpty())
{
return;
}
else if (m_pFirst == m_pLast && m_pFirst != nullptr)
{
delete m_pFirst;
m_pFirst = nullptr;
m_pLast = nullptr;
}
else
{
Node* _pDelete = m_pLast;
m_pLast = m_pLast->m_pPre;
m_pLast->m_pNext = nullptr;
delete _pDelete;
_pDelete = nullptr;
}
}
template<typename T>
void DoubleList<T>::Delete(Node* pElement_)
{
if (pElement_ == nullptr)
{
return;
}
Node* _pPre = pElement_->m_pPre;
Node* _pNext = pElement_->m_pNext;
if (_pPre == nullptr)
{
DeleteFirst();
}
else if (_pNext == nullptr)
{
DeleteLast();
}
else
{
_pPre->m_pNext = _pNext;
_pNext->m_pPre = _pPre;
delete pElement_;
pElement_ = nullptr;
}
}
template<typename T>
void DoubleList<T>::Delete(const T& nValue_)
{
Node* _pDeletedElement = Find([nValue_](const T& nT_)->bool
{
if (nT_ == nValue_)
{
return true;
}
else
{
return false;
}
});
Delete(_pDeletedElement);
}
template<typename T>
typename DoubleList<T>::Node* DoubleList<T>::GetFirst() const
{
return m_pFirst;
}
template<typename T>
typename DoubleList<T>::Node* DoubleList<T>::GetLast() const
{
return m_pLast;
}
template<typename T>
typename DoubleList<T>::Node* DoubleList<T>::Find(std::function<bool(const T&)> predictFunction_) const
{
Node *_pCur = m_pFirst;
while (_pCur)
{
if (predictFunction_(_pCur->GetValue()) == true)
{
break;
}
_pCur = _pCur->m_pNext;
}
return _pCur;
}
template<typename T>
typename DoubleList<T>::Node* DoubleList<T>::GetNext(Node* pElement_) const
{
if (pElement_ == nullptr)
{
return nullptr;
}
return pElement_->m_pNext;
}
template<typename T>
typename DoubleList<T>::Node* DoubleList<T>::GetPre(Node* pElement_) const
{
if (pElement_ == nullptr)
{
return nullptr;
}
return pElement_->m_pPre;
}
}
}
}
#endif