-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlist.cpp
465 lines (424 loc) · 9.56 KB
/
list.cpp
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
#include <iostream>
#include <stdexcept>
using namespace std;
template <typename T>
class list
{
public:
// 无参构造,创建空链表
list() : m_head(NULL), m_tail(NULL) {}
// 拷贝构造
list(list const &that) : m_head(NULL), m_tail(NULL)
{
for (node *pnode = that.m_head; pnode; pnode = pnode->m_next)
{
push_back(pnode->m_data);
}
}
// 析构
~list()
{
clear();
}
// 链表判空
bool isEmpty()
{
return m_head == NULL && m_tail == NULL;
}
// 添加头节点
void push_front(T const &data)
{
m_head = new node(NULL, data, m_head);
if (m_head->m_next)
{
m_head->m_next->m_prev = m_head;
}
else
{
m_tail = m_head;
}
}
// 删除链表头节点
void pop_front()
{
if (isEmpty())
{
return;
}
node *pnode = m_head->m_next;
delete m_head;
if (pnode)
pnode->m_prev = NULL;
else
m_tail = NULL;
m_head = pnode;
}
// 获取头节点元素
T &front()
{
if (isEmpty())
throw underflow_error("null node");
return m_head->m_data;
}
T const &front() const
{
return const_cast<list *>(this)->front();
}
// 添加尾节点
void push_back(T const &data)
{
m_tail = new node(m_tail, data, NULL);
if (m_tail->m_prev)
m_tail->m_prev->m_next = m_tail;
else
m_head = m_tail;
}
// 删除尾节点
void pop_back()
{
if (isEmpty())
return;
node *pnode = m_tail->m_prev;
delete m_tail;
if (pnode)
pnode->m_next = NULL;
else
m_head = NULL;
m_tail = pnode;
}
// 获取尾节点数据
T &back()
{
if (isEmpty())
throw underflow_error("null node");
return m_tail->m_data;
}
T const &back() const
{
return const_cast<list *>(this)->back();
}
// 清空链表
void clear()
{
while (!isEmpty())
{
pop_front();
}
}
// 获取链表大小
size_t size()
{
size_t i = 0;
for (node *pnode = m_head; pnode; pnode = pnode->m_next)
{
++i;
}
return i;
}
private:
// 节点类
class node
{
public:
node(node *prev, T const &data,
node *next) : m_prev(prev), m_data(data), m_next(next) {}
node *m_prev; // 前指针
T m_data; // 节点数据
node *m_next; // 后指针
};
node *m_head; // 链表头
node *m_tail; // 链表尾
// 在接触迭代器知识前,姑且这样用
// friend ostream &operator<<(ostream &os, list<int> &l);
public:
// 正向非常迭代类
class iterator
{
public:
iterator(node *start, node *cur,
node *end) : m_start(start), m_cur(cur), m_end(end) {}
T &operator*()
{
if (m_cur == NULL)
throw underflow_error("null node");
return m_cur->m_data;
}
iterator &operator++()
{
if (m_cur == NULL)
m_cur = m_start;
else
m_cur = m_cur->m_next;
return *this;
}
iterator operator--()
{
if (m_cur == NULL)
m_cur = m_end;
else
m_cur = m_cur->m_prev;
return *this;
}
bool operator==(iterator const &that) const
{
return m_start == that.m_start && m_cur == that.m_cur && m_end == that.m_end;
}
bool operator!=(iterator const &that) const
{
return !(*this == that);
}
private:
node *m_start; // 开始指向
node *m_cur; // 当前指向
node *m_end; // 终止指向
friend class list;
};
// 获取起始迭代器(遍历链表)
iterator begin()
{
return iterator(m_head, m_head, m_tail);
}
// 获取终止迭代器(结束标志)
iterator end()
{
return iterator(m_head, NULL, m_tail);
}
// 迭代器指向的位置添加节点
void insert(iterator const &loc, T const &data)
{
if (loc == end())
{
push_back(data);
}
else
{
node *pnode = new node(loc.m_cur->m_prev, data, loc.m_cur);
if (pnode->m_prev)
pnode->m_prev->m_next = pnode;
else
m_head = pnode;
pnode->m_next->m_prev = pnode;
}
}
// 删除迭代器指向的节点
void erase(iterator const &loc)
{
if (loc == end())
return;
node *pdel = loc.m_cur;
if (pdel->m_prev)
pdel->m_prev->m_next = pdel->m_next;
else
m_head = pdel->m_next;
if (pdel->m_next)
pdel->m_next->m_prev = pdel->m_prev;
else
m_tail = pdel->m_prev;
delete pdel;
}
// 正向常迭代类
class const_iterator
{
public:
const_iterator(iterator const &it) : m_it(it) {}
T const &operator*()
{
return *m_it;
}
const_iterator &operator++()
{
++m_it;
return *this;
}
const_iterator &operator--()
{
--m_it;
return *this;
}
bool operator==(const_iterator const &that) const
{
return m_it == that.m_it;
}
bool operator!=(const_iterator const &that) const
{
return !(*this == that);
}
private:
iterator m_it;
};
// 获取起始常迭代器
const_iterator begin() const
{
return iterator(m_head, m_head, m_tail);
}
// 获取终止常迭代器
const_iterator end() const
{
return iterator(m_head, NULL, m_tail);
}
};
// 比较查找(利用==)
template <typename IT, typename T>
IT find(IT const &a, IT const &b, T const &data)
{
for (IT it = a; it != b; ++it)
{
if (*it == data)
{
return it;
}
}
return b;
}
// 以上代码模拟容器
// -----------------------
// 以下代码模拟普通用户使用
// ostream &operator<<(ostream &os, list<int> &l)
// {
// for (list<int>::node *pnode = l.m_head; pnode; pnode = pnode->m_next)
// {
// os << pnode->m_data << ' ';
// }
// return os;
// }
void print(string const &str, list<int> &l)
{
cout << str << endl;
typedef list<int>::iterator IT;
for (IT it = l.begin(); it != l.end(); ++it)
{
cout << *it << ' ';
}
cout << endl
<< "--------------------" << endl;
}
// 快速排序("<")
template <typename IT>
void sort(IT const &begin, IT const &end)
{
IT p = begin;
IT last = end;
--last;
for (IT i = begin, j = last; i != j;)
{
while (i != p && *i < *p)
{
++i;
}
if (i != p)
{
swap(*i, *p);
p = i;
}
while (j != p && *p < *j)
{
--j;
}
if (j != p)
{
swap(*p, *j);
p = j;
}
}
IT it = begin;
++it;
if (p != begin && p != it)
sort(begin, p);
it = p;
++it;
if (it != end && it != last)
sort(it, end);
}
// 快速排序(比较器)
template <typename IT, typename CMP>
void sort(IT const &begin, IT const &end, CMP cmp)
{
IT p = begin;
IT last = end;
--last;
for (IT i = begin, j = last; i != j;)
{
while (i != p && cmp(*i, *p))
{
++i;
}
if (i != p)
{
swap(*i, *p);
p = i;
}
while (j != p && cmp(*p, *j))
{
--j;
}
if (j != p)
{
swap(*p, *j);
p = j;
}
}
IT it = begin;
++it;
if (p != begin && p != it)
sort(begin, p, cmp);
it = p;
++it;
if (it != end && it != last)
sort(it, end, cmp);
}
// 比较类
class CMP
{
public:
bool operator()(int const &a, int const &b)
{
return a > b;
}
};
int main()
{
list<int> ls;
for (size_t i = 0; i < 5; i++)
{
ls.push_front(10 + i);
}
for (size_t i = 0; i < 5; i++)
{
ls.push_back(100 + i);
}
// cout << ls << endl;
print("添加节点后:", ls);
ls.pop_front();
ls.pop_back();
// cout << ls << endl;
print("删除头尾节点后:", ls);
ls.insert(++ls.begin(), 1000);
print("在迭代器指向的位置添加节点后:", ls);
ls.erase(ls.begin());
print("删除迭代器指向的节点后:", ls);
typedef list<int>::iterator IT;
IT it = ls.begin();
*it = 800;
print("更改迭代器指向的节点后:", ls);
IT fit = find(ls.begin(), ls.end(), 100);
if (fit != ls.end())
ls.erase(fit);
print("找到100并删除后:", ls);
/*
const list<int> cls(ls);
typedef list<int>::const_iterator CIT;
for (CIT cit = cls.begin(); cit != cls.end(); ++cit)
{
cout << *cit << ' ';
}
cout << endl
<< "--------------------" << endl;
CIT cit = cls.begin();
// *cit = 900; // 不可修改,会报错
*/
// sort(ls.begin(), ls.end());
CMP cmp; // 比较器
sort(ls.begin(), ls.end(), cmp);
print("排序后:", ls);
return 0;
}