-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzjq_list.h
531 lines (453 loc) · 11.3 KB
/
zjq_list.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#ifndef _ZJQ_LIST_H_
#define _ZJQ_LIST_H_
namespace lis{
template<typename T>
class list{
private:
struct Node{
Node() : pre(nullptr) , next(nullptr) , data(0) {}
T data;
Node *pre , *next;
};
public:
size_t length;
Node *start;
Node *over;
using p_node = Node*;
using size_type = size_t;
p_node nullnode;
class iterator{
public:
p_node no;
iterator(p_node p = nullptr) : no(p) {}
bool operator !=(const iterator& it) const { return no != it.no; }
bool operator ==(const iterator& it) const { return no == it.no; }
iterator operator ++() { no = no->next; return no; }
iterator operator --() { no = no->pre; return no; }
iterator operator ++(int) { iterator temp = no; no = no->next; return temp; }
iterator operator --(int) { iterator temp = no; no = no->pre; return temp; }
iterator operator + (size_type n) {
p_node tno = no;
for (size_type i = 0; i < n; ++i){
tno = tno->next;
}
return tno;
}
iterator operator - (size_type n) {
p_node tno = no;
for (size_type i = 0; i < n; ++i){
tno = tno->pre;
}
return tno;
}
iterator operator += (size_type n) {
for (size_type i = 0; i < n; ++i){
no = no->next;
}
return no;
}
iterator operator -= (size_type n) {
for (size_type i = 0; i < n; ++i){
no = no->pre;
}
return no;
}
T& operator *() { return no->data; }
};
class reverse_iterator{
public:
p_node no;
reverse_iterator(p_node p = nullptr) : no(p) {}
bool operator !=(const reverse_iterator& it) const { return no != it.no; }
bool operator ==(const reverse_iterator& it) const { return no == it.no; }
reverse_iterator operator ++() { no = no->pre; return no; }
reverse_iterator operator --() { no = no->next; return no; }
reverse_iterator operator ++(int) { reverse_iterator temp = no; no = no->pre; return temp; }
reverse_iterator operator --(int) { reverse_iterator temp = no; no = no->next; return temp; }
reverse_iterator operator + (size_type n) {
p_node tno = no;
for (size_type i = 0; i < n; ++i){
tno = tno->pre;
}
return tno;
}
reverse_iterator operator - (size_type n) {
p_node tno = no;
for (size_type i = 0; i < n; ++i){
tno = tno->next;
}
return tno;
}
reverse_iterator operator += (size_type n) {
for (size_type i = 0; i < n; ++i){
no = no->pre;
}
return no;
}
reverse_iterator operator -= (size_type n) {
for (size_type i = 0; i < n; ++i){
no = no->next;
}
return no;
}
T& operator *() { return no->data; }
};
class const_iterator{
public:
p_node no;
const_iterator(p_node p = nullptr) : no(p) {}
bool operator !=(const_iterator& it) const { return no != it.no; }
bool operator ==(const_iterator& it) const { return no == it.no; }
const_iterator operator ++() { no = no->next; return no; }
const_iterator operator --() { no = no->pre; return no; }
const_iterator operator ++(int) { const_iterator temp = no; no = no->next; return temp; }
const_iterator operator --(int) { const_iterator temp = no; no = no->pre; return temp; }
const_iterator operator + (size_type n) {
p_node tno = no;
for (size_type i = 0; i < n; ++i){
tno = tno->next;
}
return tno;
}
const_iterator operator - (size_type n) {
p_node tno = no;
for (size_type i = 0; i < n; ++i){
tno = tno->pre;
}
return tno;
}
const_iterator operator += (size_type n) {
for (size_type i = 0; i < n; ++i){
no = no->next;
}
return no;
}
const_iterator operator -= (size_type n) {
for (size_type i = 0; i < n; ++i){
no = no->pre;
}
return no;
}
const T& operator *() const { return no->data; }
};
class const_reverse_iterator{
public:
p_node no;
const_reverse_iterator(p_node p = nullptr) : no(p) {}
bool operator !=(const_reverse_iterator& it) const { return no != it.no; }
bool operator ==(const_reverse_iterator& it) const { return no == it.no; }
const_reverse_iterator operator ++() { no = no->pre; return no; }
const_reverse_iterator operator --() { no = no->next; return no; }
const_reverse_iterator operator ++(int) { reverse_iterator temp = no; no = no->pre; return temp; }
const_reverse_iterator operator --(int) { reverse_iterator temp = no; no = no->next; return temp; }
const_reverse_iterator operator + (size_type n) {
p_node tno = no;
for (size_type i = 0; i < n; ++i){
tno = tno->pre;
}
return tno;
}
const_reverse_iterator operator - (size_type n) {
p_node tno = no;
for (size_type i = 0; i < n; ++i){
tno = tno->next;
}
return tno;
}
const_reverse_iterator operator += (size_type n) {
for (size_type i = 0; i < n; ++i){
no = no->pre;
}
return no;
}
const_reverse_iterator operator -= (size_type n) {
for (size_type i = 0; i < n; ++i){
no = no->next;
}
return no;
}
const T& operator *() const { return no->data; }
};
list() : nullnode(new Node()) , length(0) , start(nullptr) , over(nullptr) {}
list(size_type n , const T& val) : list() {
for (size_type i = 0; i < n; ++i){
push_back(val);
}
}
list(iterator a , iterator z) : list() {
for(iterator it = a; it != z; ++it){
push_back(*it);
}
}
list(list<T>& l) : list() {
for(iterator it = l.begin(); it != l.end(); ++it){
push_back(*it);
}
}
~list() { clear(); delete nullnode; }
iterator begin() const { return start; }
iterator end() const { return over->next; }
const_iterator cbegin() const { return start; }
const_iterator cend() const { return over->next; }
reverse_iterator rbegin() const { return over; }
reverse_iterator rend() const { return start->pre; }
const_reverse_iterator crbegin() const { return over; }
const_reverse_iterator crend() const { return start->pre; }
bool empty() { return !length; }
size_type size() const { return length; }
T& front() { return start->data; }
T& back() { return over->data; }
void assign(size_type n ,const T& val);
void push_front(const T& val);
void push_back(const T& val);
void pop_front();
void pop_back();
void insert(iterator it , const T& val);
void insert(iterator it , size_type n , const T& val);
iterator erase(iterator it);
iterator erase(iterator a , iterator z);
void resize(size_type n);
void reverse();
void swap(list<T>& l);
void sort();
void clear();
private:
p_node merge_sort(p_node a);
p_node merge(p_node a , p_node b);
};
template<typename T>
inline void list<T>::assign(size_type n ,const T& val){
for (size_type i = 0; i < n; ++i){
push_back(val);
}
}
template<typename T>
inline void list<T>::push_front(const T& val){
p_node node = new Node();
node->data = val;
if (length == 0){
start = node;
over = node;
node->pre = nullnode;
node->next = nullnode;
nullnode->pre = node;
nullnode->next = node;
++length;
return;
}
start->pre = node;
node->next = start;
start = node;
node->pre = nullnode;
nullnode->next = node;
++length;
}
template<typename T>
inline void list<T>::push_back(const T& val){
p_node node = new Node();
if (length == 0){
push_front(val);
return;
}
over->next = node;
node->pre = over;
over = node;
node->data = val;
node->next = nullnode;
nullnode->pre = node;
++length;
}
template<typename T>
inline void list<T>::pop_front(){
if (length == 1){
delete start;
start = nullptr;
over = nullptr;
length = 0;
return;
}
p_node t = start;
start = start->next;
start->pre = nullnode;
delete t;
--length;
}
template<typename T>
inline void list<T>::pop_back(){
if (length == 1){
pop_front();
return;
}
p_node t = over;
over = over->pre;
over->next = nullnode;
delete t;
--length;
}
template<typename T>
inline void list<T>::insert(iterator it , const T& val){
if (it == begin()){
push_front(val);
return;
}
else if(it == end()){
push_back(val);
return;
}
p_node node = new Node();
node->data = val;
it.no->pre->next = node;
node->pre = it.no->pre;
node->next = it.no;
it.no->pre = node;
++length;
}
template<typename T>
inline void list<T>::insert(iterator it , size_type n , const T& val){
for(size_type i = 0; i < n; ++i){
insert(it , val);
}
}
template<typename T>
inline typename list<T>::iterator list<T>::erase(iterator it){
if(it == end())
return it;
else if (it == begin()){
pop_front();
return begin();
}
else if(it - 1 == end()){
pop_back();
return end();
}
p_node t = it.no;
it.no->pre->next = it.no->next;
it.no->next->pre = it.no->pre;
delete t;
--length;
return t->next;
}
template<typename T>
inline typename list<T>::iterator list<T>::erase(iterator a , iterator z){
for(iterator it = a; it != z; ++it){
erase(it);
}
return z;
}
template<typename T>
inline void list<T>::resize(size_type n){
if(n < length){
for (size_type i = 0; i < length - n; ++i){
pop_back();
}
}
else if(n > length){
for (size_type i = 0; i < n - length; ++i){
push_back(0);
}
}
}
template<typename T>
inline void list<T>::reverse(){
p_node i = start , j = over->next;
while(i != j){
p_node temp = i->next;
i->next = i->pre;
i->pre = temp;
i = temp;
}
p_node temp = over;
over = start;
start = temp;
}
template<typename T>
inline void list<T>::swap(list<T>& l){
p_node tp = this->start;
this->start = l.start;
l.start = tp;
tp = this->over;
this->over = l.over;
l.over = tp;
size_type tl = this->length;
this->length = l.length;
l.length = tl;
}
template<typename T>
void list<T>::sort(){
nullnode->pre = nullptr;
nullnode->next = nullptr;
start->pre = nullptr;
over->next = nullptr;
start = merge_sort(start);
p_node temp = start;
while(temp->next){
temp = temp->next;
}
over = temp;
nullnode->next = start;
nullnode->pre = over;
start->pre = nullnode;
over->next = nullnode;
}
template<typename T>
typename list<T>::p_node list<T>::merge_sort(p_node h){
if (!h || !h->next){
return h;
}
p_node a = h , b = h , p = nullptr;
if (a && a->next){
p = a;
a = a->next;
b = b->next->next;
}
p->next->pre = nullptr;
p->next = nullptr;
p_node lh = merge_sort(h);
p_node rh = merge_sort(a);
return merge(lh , rh);
}
template<typename T>
typename list<T>::p_node list<T>::merge(p_node a , p_node b){
p_node sk = nullptr , rk = nullptr;
if (a->data <= b->data){
sk = a;
rk = a;
a = a->next;
}
else{
sk = b;
rk = b;
b = b->next;
}
while(a && b){
if (a->data <= b->data){
rk->next = a;
a->pre = rk;
rk = rk->next;
a = a->next;
}
else{
rk->next = b;
b->pre = rk;
rk = rk->next;
b = b->next;
}
}
if (a){
rk->next = a;
a->pre = rk;
}
else{
rk->next = b;
b->pre = rk;
}
return sk;
}
template<typename T>
inline void list<T>::clear(){
for (size_type i = 0; i < length; ++i){
pop_back();
}
}
}
#endif