-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDList (template).cpp
More file actions
319 lines (294 loc) · 4.65 KB
/
DList (template).cpp
File metadata and controls
319 lines (294 loc) · 4.65 KB
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
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
//template<class T>
/*typedef struct ListNode
{
T _data;
ListNode<T>* _next;
ListNode<T>* _prev;
ListNode(const T& d)
: _data(d)
, _next(NULL)
, _prev(NULL)
{
}
}Node; */ //我刚开始这么写 错了 其实我之前写能正确是因为 那时候ListNode既是类型名又是类型 但是要注意有了模板之后类型名和类型不同
template<class T> //在這里 ListNode是类型名 ListNode<T>是类型 我们用到类型名的地方只有声明类 构造函数名和拷贝构造函数名
struct ListNode
{
T _data;
ListNode<T>* _next;
ListNode<T>* _prev;
ListNode(const T& d) //构造函数
: _data(d)
, _next(NULL)
, _prev(NULL)
{
}
}; //所以上面就不能使用typedef 因为typdef后面必须是一个类型 而不是类型名 给类型重新起一个名字
template<class T>
class List
{
typedef ListNode<T> Node;
private:
Node* _head;
Node* _tail;
public:
List()
:_head(NULL)
,_tail(NULL)
{
}
List(const List<T>& s)
:_head(NULL)
, _tail(NULL)
{
if (s._head)
{
Node* cur = s._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
}
List<T>& operator=(List<T>& s)
{
if (_head)
{
Destory();
}
else if (s._head)
{
Node* cur = s._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
else
_head = _tail = NULL;
return *this;
}
void Destory()
{
if (_head == NULL)
{
cout << "链表为空" << endl;
cout << "析构" << endl;
}
else if (_head->_next == NULL)
{
cout << "析构" << endl;
delete _head;
_head = NULL;
}
else
{
Node* cur = _head;
while (cur)
{
Node* tmp = cur;
cur = cur->_next;
delete tmp;
cout << "析构" << endl;
}
}
}
~List()
{
Destory();
}
void PushBack(const T& d);
void PopBack();
void PushFront(const T& d);
void PopFront();
Node* Find(const T& d);
void Print();
bool Empty();
size_t Size();
T& Front(); //這里面我对引用作为返回值的使用依然不太了解
T& Back();
};
template<class T>
void List<T>::PushBack(const T& d)
{
if (_head == NULL)
{
_head = new Node(d);
_tail = _head;
}
else
{
_tail->_next = new Node(d);
Node* tmp;
tmp = _tail->_next;
tmp->_prev = _tail;
_tail = tmp;
}
}
template<class T>
void List<T>::PopBack() //在类外定义一个函数的形式? 返回值+所在的类型(不是类名!!)::函数(形参返回类型,形参名)
{
if (_head == NULL)
{
cout << "链表为空" << endl;
}
else if (_head->_next == NULL)
{
delete _head;
_head = NULL;
}
else
{
Node* tmp = _tail->_prev;
tmp->_next = NULL;
delete _tail;
_tail = tmp;
}
}
template<class T>
void List<T>::PushFront(const T& d)
{
if (_head == NULL)
{
_head = new Node(d);
}
else
{
Node* tmp = new Node(d);
Node* cur = _head;
_head = tmp;
tmp->_next = cur;
cur->_prev = tmp;
}
}
template<class T>
void List<T>::PopFront()
{
if (!_head)
{
cout << "链表为空" << endl;
}
else if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* tmp = _head->_next;
tmp->_prev = NULL;
delete _head;
_head = tmp;
}
}
template<class T>
ListNode<T>* List<T>:: Find(const T& d)
{
if (_head == NULL)
{
cout << "链表为空" << endl;
return NULL;
}
else
{
Node* cur = _head;
while (cur)
{
if (cur->_data == d)
return cur;
cur = cur->_next;
}
return NULL;
}
}
template<class T>
bool List<T>::Empty()
{
return _head == NULL;
}
template<class T>
size_t List<T>::Size()
{
size_t count = 0;
Node* cur = _head;
while (cur)
{
cur = cur->_next;
count++;
}
return count;
}
template<class T>
T& List<T>::Front() //我在這里为什么要返回引用
{
assert(_head);
return _head->_data;
}
template<class T>
T& List<T>::Back()
{
assert(_head); //对于这些接口 用断言判断一下 就不要if else来了
return _tail->_data;
}
template<class T>
void List<T>::Print()
{
if (_head == NULL)
{
cout << "链表为空" << endl;
}
else
{
Node* cur = _head;
while (cur)
{
cout << cur->_data << " ";
cur = cur->_next;
}
cout << endl;
}
}
void test()
{
List<int> s1;
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.PushBack(5);
s1.PushBack(6);
s1.Print();
s1.PopBack();
s1.PopBack();
s1.PopBack();
s1.PopBack();
s1.Print();
s1.PushFront(0);
s1.PushFront(0);
s1.PushFront(0);
s1.Print();
s1.PopFront();
s1.PopFront();
s1.PopFront();
s1.Print();
cout << s1.Size()<<endl;
cout << s1.Empty()<<endl;
cout<<s1.Back()<<endl;
cout << s1.Front()<<endl;
List<int> s2(s1);
s2.Print();
List<int>s3;
s3 = s2;
s3.Print();
}
int main()
{
test();
return 0;
}