-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.h
203 lines (193 loc) · 4.63 KB
/
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
//
// main.cpp
// List
//
// Created by 杨涛睿 on 2020/8/9.
// Copyright © 2020 杨涛睿. All rights reserved.
//
#include <iostream>
typedef int Rank;
#define Posi(T) ListNode<T>*
using namespace std;
template<typename T>
struct ListNode{
T data;
Posi(T) pred; Posi(T) succ; //前驱和后继
ListNode(){}
ListNode(T e,Posi(T)p = NULL,Posi(T)s = NULL):data(e),pred(p),succ(s){}
Posi(T) insertAsPred(T const&e);
Posi(T) insertAsSucc(T const&e);
};
template<typename T>
Posi(T) ListNode<T>::insertAsPred(const T &e){
Posi(T) p = new ListNode(e,this->pred,this);
pred->succ = p;
pred = p;
return p;
/*
不能在函数栈中创建结点,会被其他函数定义的变量覆盖!
ListNode p(e,this->pred,this);
pred->succ = &p;
pred = &p;
return &p;
*/
}
template<typename T>
class List{
private:
int _size;
Posi(T) header;
Posi(T) trailer;
protected:
void init();
int clear();
void copyNodes ( Posi(T), int n);
public:
T remove(Posi(T) p);
/*重载下标操作符
在函数后加const的含义是:该函数不修改对象内数据成员的值
*/
T& operator[] ( Rank r ) const{
Posi(T) p = header->succ;
while(0<r--)p=p->succ;
return p->data;
}
//插入结点
Posi(T) insertAsBefore(T const&e,Posi(T)p);
Posi(T) insertNode(T const&e);
//构造函数
List(){
init();
}
List ( List<T> const& L ); //列表整体复制
List ( List<T> const& L, Rank r, int n );//复制列表L自第R项起的第n项
List ( Posi(T) p, int n ); //复制列表自位置P起的n项
//析构函数
~ List();
//无序列表唯一化
int deduplicate();
//有序列表唯一化
int uniquify();
//列表遍历
void traverse();
//无序列表查找,与有序列表共同约定d返回不大于e的最后者
Posi(T) find(T const&e,Posi(T)p,int n);
//有序列表查找
Posi(T) search(T const&e,Posi(T)p,int n);
};
template<typename T>
void List<T>::init(){
_size = 0;
header = new ListNode<T>(0);
trailer = new ListNode<T>(0);
header->succ = trailer;
trailer->pred = header;
header->pred = NULL;
trailer->succ=NULL;
}
template<typename T>
void List<T>::copyNodes(Posi(T) p, int n){
init();
while (n--) {
insertNode(p->data);
p=p->succ;
}
}
//析构函数,先删除所有结点,再删除两个哨兵
template<typename T>
List<T>::~List(){
clear();
delete(header);
delete(trailer);
}
//删除结点
template<typename T>
T List<T>::remove(Posi(T) p){
T e = p->data;
p->pred->succ = p->succ;
p->succ->pred = p->pred;
delete(p);_size--;
return e;
}
//遍历
template<typename T>
void List<T>::traverse(){
Posi(T) p= header->succ;
while(p!=trailer){
cout << p->data <<endl;
p = p->succ;
}
}
//在结点前插入
template<typename T>
Posi(T) List<T>::insertAsBefore(const T &e, Posi(T) p){
_size++;
return p->insertAsPred(e);
}
//在列表末尾新增结点
template<typename T>
Posi(T) List<T>::insertNode(const T &e){
_size++;
Posi(T) p = new ListNode<T>(e,trailer->pred,trailer);
trailer->pred->succ = p;
trailer ->pred = p;
return p;
/*
_size++;
ListNode<T>node(e);
node.pred = trailer->pred;
node.succ = trailer;
trailer->pred = &node;
node.pred->succ = &node;
*/
}
//清空结点
template<typename T>
int List<T>::clear(){
int oldSize = _size;
while(_size>0)
remove(header->succ);
return oldSize;
}
//无序列表唯一化,思想:前缀无重复元素,后缀加入前缀
template <typename T>
int List<T>::deduplicate(){
if(_size<2)return 0;
int oldSize = _size;
Posi(T) p = header->succ;Rank r = 1;
while(trailer!=(p=p->succ)){
Posi(T) q = find(p->data, p, r);
q?remove(q):r++;
}
return oldSize - _size;
}
//有序列表唯一化,思想:前缀无重复元素,后缀加入前缀
template<typename T>
int List<T>::uniquify(){
if(_size<2)return 0;
int oldSize = _size;
Posi(T) p = header->succ;
Posi(T) q ;
while(trailer!=(q=p->succ)){
if(p->data!=q->data)p=q;
else remove(q);
}
return oldSize - _size;
}
//无序列表查找,n在唯一化时起作用
template<typename T>
Posi(T) List<T>::find(T const&e,Posi(T)p,int n){
while(n-->0){
if(e==(p=p->pred)->data)return p;
}
return NULL;
}
//有序列表查找
template<typename T>
Posi(T) List<T>::search(T const&e,Posi(T)p,int n){
while(n-->0){
if(((p=p->pred)->data) <=e)break;
return p;
}
return p;
}