SimpleList.hpp
7.75 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
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
#ifndef __LIST_HPP__
#define __LIST_HPP__
#include<iostream>
// 先声明链表类,便于node将其声明为友元
template<typename T> class SimpleList;
// 链表节点类
template<typename T>
class node
{
public:
node() : next(nullptr){}
node(T val) : data(val), next(nullptr) {}
private:
T data;
node* next;
friend class SimpleList<T>;
};
/*
链表操作
1、删除节点
2、添加节点
3、
*/
template<typename T>
class SimpleList
{
public:
SimpleList(); // 构造函数
SimpleList(const SimpleList<T>& l); // 拷贝构造
~SimpleList(); // 析构函数
SimpleList<T>& operator= (const SimpleList<T>& l); // 拷贝赋值
void insert(int index, T val); // 在index处插入结点
void remove(int index); // 删除index处结点
T get_node(int index); // 获取index处结点值
int find(int value); // 查找值value,找到返回index,找不到返回-1
int size(); // 获取链表长度
void push_back(T val); // 在链表尾部插入数据
T front(); // 获取第一个
void pop_front(); // 删除第一个
void clear(); // 清空
bool empty();
T begin(); // 第一个
private:
node<T>* head_ptr; // 链表头指针
int length; // 链表长度
};
// 构造函数
template<typename T>
SimpleList<T>::SimpleList()
{
this->head_ptr = nullptr;
this->length = 0;
}
// 拷贝构造
template<typename T>
SimpleList<T>::SimpleList(const SimpleList<T>& l)
{
if(l.head_ptr == nullptr)
{
this->head_ptr = nullptr;
this->length = 0;
}
else
{
this->head_ptr = new node<T>;
node<T>* p1 = this->head_ptr;
node<T>* p2 = nullptr;
// 拷贝链表
for(p2 = l.head_ptr; p2->next != nullptr; p2 = p2->next)
{
p1->data = p2->data;
p1->next = new node<T>;
p1 = p1->next;
}
p1->data = p2->data;
p1->next = nullptr;
this->length = l.length;
}
}
// 拷贝赋值
template<typename T>
SimpleList<T>& SimpleList<T>::operator= (const SimpleList<T>& l)
{
if(this->head_ptr == nullptr) // 原链表空
{
if(l.head_ptr == nullptr)
{
this->head_ptr = nullptr;
this->length = 0;
}
else
{
this->head_ptr = new node<T>;
node<T>* p1 = this->head_ptr;
node<T>* p2 = nullptr;
// 拷贝链表
for(p2 = l.head_ptr; p2->next != nullptr; p2 = p2->next)
{
p1->data = p2->data;
p1->next = new node<T>;
p1 = p1->next;
}
p1->data = p2->data;
p1->next = nullptr;
this->length = l.length;
}
}
else // 原链表非空
{
if(l.head_ptr == nullptr)
{
// 清空链表
node<T>* p1 = this->head_ptr;
node<T>* p2 = p1->next;
while(p2 != nullptr)
{
delete p1;
p1 = p2;
p2 = p2->next;
}
delete p1;
this->length = 0;
}
else
{
if(l.length > this->length) // 待复制的链表更长,先将原链表填满再申请空间
{
node<T>* p1 = this->head_ptr;
node<T>* p2 = l.head_ptr;
for(int i = 0; i < this->length - 1; i++)
{
p1->data = p2->data;
p1 = p1->next;
p2 = p2->next;
}
p1->next = new node<T>;
for(int i = this->length - 1; i < l.length - 1; i++)
{
p1->data = p2->data;
p1->next = new node<T>;
p1 = p1->next;
p2 = p2->next;
}
p1->data = p2->data;
p1->next = nullptr;
}
else // 待复制的链表更短或相等,更短时需要释放原链表多余空间
{
node<T>* p1 = this->head_ptr;
node<T>* p2 = l.head_ptr;
// 复制数据
for(int i = 0; i < l.length; i++)
{
p1->data = p2->data;
p1 = p1->next;
p2 = p2->next;
}
// 删除多余结点
while(p1 != nullptr)
{
p2 = p1->next;
delete p1;
p1 = p2;
}
}
}
this->length = l.length;
}
return *this;
}
// 在index处插入结点
template<typename T>
void SimpleList<T>::insert(int index, T val)
{
if((index > this->length)) // 超过索引,最多可以插到当前结点的下一个结点,否则就是超过索引
{
throw runtime_error("index out of this SimpleList`s range");
}
else if((this->head_ptr == nullptr) && (index == 0)) // 插在空链表的头
{
this->head_ptr = new node<T>;
this->head_ptr->next = nullptr;
this->head_ptr->data = val;
this->length++;
}
else // 一般情况
{
node<T>* p1 = this->head_ptr;
node<T>* p2 = new node<T>;
for(int i = 0; i < index - 1; i++)
{
p1 = p1->next;
}
p2->data = val;
p2->next = p1->next;
p1->next = p2;
this->length++;
}
}
// 删除index处结点
template<typename T>
void SimpleList<T>::remove(int index)
{
node<T>* p1 = this->head_ptr;
node<T>* p2 = nullptr;
for(int i = 0; i < index - 1; i++)
{
p1 = p1->next;
}
p2 = p1->next->next;
delete p1->next;
p1->next = p2;
this->length--;
}
// 获取index处结点值
template<typename T>
T SimpleList<T>::get_node(int index)
{
if(index > this->length - 1) // 超过索引
{
throw runtime_error("index out of this SimpleList`s range");
}
node<T>* p1 = this->head_ptr;
for(int i = 0; i < index; i++){
p1 = p1->next;
}
return p1->data;
}
// 获取第一个
template<typename T>
T SimpleList<T>::front()
{
return get_node(0);
}
// 删除index处结点
template<typename T>
void SimpleList<T>::pop_front()
{
remove(0);
}
// 删除index处结点
template<typename T>
void SimpleList<T>::clear()
{
node<T>* p;
while (head_ptr != NULL)
{
p = head_ptr;
head_ptr = head_ptr->next;
delete p;
p = nullptr;
}
}
// 删除index处结点
template<typename T>
T SimpleList<T>::begin(){
return head_ptr;
}
// 查找值value,找到返回index,找不到返回-1
template<typename T>
int SimpleList<T>::find(int value)
{
node<T>* p1 = this->head_ptr;
for(int i = 0; i < this->length; i++)
{
if(p1->data == value)
{
return i;
}
p1 = p1->next;
}
return -1;
}
// 获取链表长度
template<typename T>
int SimpleList<T>::size()
{
return this->length;
}
// 链表是否为空
template<typename T>
bool SimpleList<T>::empty()
{
return size() == 0 ? true : false ;
}
// 在链表尾部插入数据
template<typename T>
void SimpleList<T>::push_back(T val)
{
if(this->head_ptr == nullptr) // 链表为空
{
this->head_ptr = new node<T>;
this->head_ptr->data = val;
this->head_ptr->next = nullptr;
}
else
{
node<T>* p1 = this->head_ptr;
node<T>* p2 = new node<T>;
p2->data = val;
p2->next = nullptr;
while(p1->next != nullptr)
{
p1 = p1->next;
}
p1->next = p2;
}
this->length++;
}
// 析构函数
template<typename T>
SimpleList<T>::~SimpleList(){
// 清空链表
clear();
}
#endif