-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChain.hpp
executable file
·311 lines (247 loc) · 9.47 KB
/
Chain.hpp
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
/*
Copyright (c) 2017 waynezxcv <liuweiself@126.com>
https://github.com/waynezxcv/Playground
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef LinkList_hpp
#define LinkList_hpp
#include <iostream>
#include "LinearList.hpp"
#pragma mark - 链式描述的线性表
namespace LWTL {
//链表节点
template <typename T>
class ChainNode {
public:
ChainNode<T>* next;
T element;
//构造函数
ChainNode() {};
ChainNode(const T& t) {
element = t;
next = NULL;
}
ChainNode(const T& t,ChainNode<T>* n) {
element = t;
next = n;
}
};
//单向链表
template <typename T>
class Chain : public LinearList<T> {
protected:
ChainNode<T>* firstNode;
int ListSize;
void checkIndex (const int& theIndex ) const {
if (theIndex < 0 || theIndex >= ListSize) {
throw IllegalParameterValue("the index must between [0,ListSize)");
}
}
public:
//构造函数
//capacity用于与ArrayList兼容
Chain(int initCapacity) {
if (initCapacity < 1) {
throw IllegalParameterValue("init capacity must be > 0");
}
firstNode = NULL;
ListSize = 0;
}
//拷贝构造函数
Chain(const Chain<T>& rhs) {
ListSize = rhs.ListSize;
//链表为空
if (ListSize == 0) {
firstNode = NULL;
} else {
//需要复制每一个节点
ChainNode<T>* sourceNode = rhs.firstNode;
this -> firstNode = new ChainNode<T> (rhs.firstNode -> element);
sourceNode = sourceNode -> next;
ChainNode<T>* targetNode = this -> firstNode;
while (sourceNode != NULL) {
//复制
targetNode -> next = new ChainNode<T> (sourceNode -> element);
//指向下一个节点
sourceNode = sourceNode -> next;
targetNode = targetNode -> next;
}
//最后一个节点的next指向NULL
targetNode -> next = NULL;
}
}
//拷贝赋值运算符
Chain& operator = (const Chain<T>& rhs) {
if (this == &rhs) {
return *this;
}
while (firstNode != NULL) {
//清除首节点
ChainNode<T>* nextNode = firstNode -> next;
delete firstNode;
firstNode = nextNode;
}
ListSize = rhs.ListSize;
//链表为空
if (ListSize == 0) {
firstNode = NULL;
} else {//需要复制每一个节点
//复制第一个节点
ChainNode<T>* sourceNode = rhs.firstNode;
this -> firstNode = new ChainNode<T> (rhs.firstNode);
sourceNode = sourceNode -> next;
ChainNode<T>* targetNode = this -> firstNode;
while (sourceNode != NULL) {
//复制
targetNode -> next = new ChainNode<T> (sourceNode -> element);
//指向下一个节点
sourceNode = sourceNode -> next;
targetNode = targetNode -> next;
}
//最后一个节点的next指向NULL
targetNode -> next = NULL;
}
}
//析构函数,要逐个清除链表的节点
//实现策略,重复清除首元素节点,直到链表为空
~Chain() {
while (firstNode != NULL) {
//清除首节点
ChainNode<T>* nextNode = firstNode -> next;
delete firstNode;
firstNode = nextNode;
}
}
//ADT
//确定线性表是否为空
virtual bool empty() const override {
return ListSize == 0;
}
//确定线性表长度
virtual int size() const override {
return ListSize;
}
//按一个给定的索引查找一个元素
virtual T& get(const int& index) const override {
checkIndex(index);
ChainNode<T>* currentNode = firstNode;
int count = 0;
while (count != index) {
currentNode = currentNode -> next;
count ++;
}
return currentNode -> element;
}
//按一个给定的元素查找他的索引
virtual int indexOf(const T& element) const override {
ChainNode<T>* currentNode = firstNode;
int theIndex = 0;
while (currentNode -> next != NULL &&
currentNode -> element != element ) {
currentNode = currentNode -> next;
theIndex ++;
}
if (currentNode == NULL) {
return -1;
}
return theIndex;
}
//按一个给定索引删除一个元素
virtual void erase(const int& theIndex) override {
checkIndex(theIndex);
//如果需要删除的节点在开头位置
if (theIndex == 0) {
//清除首节点
ChainNode<T>* nextNode = firstNode -> next;
delete firstNode;
firstNode = nextNode;
} else {
ChainNode<T>* p = firstNode;
//寻找要删除节点的前驱节点
for (int i = 0; i < theIndex - 1; i ++) {
p = p -> next;
}
ChainNode<T>* theNode = p -> next;
p -> next = p -> next -> next;
delete theNode;
}
ListSize --;
}
//按一个给定索引插入一个元素
virtual void insert( const int& theIndex,const T& element) override {
//检查索引
if (theIndex < 0 || theIndex > ListSize) {
throw IllegalParameterValue("insert index must beween [0,ListSize]");
}
//在起始位置插入
if (theIndex == 0) {
ChainNode<T>* theNode = new ChainNode<T>(element);
theNode -> next = firstNode;
firstNode = theNode;
} else {
ChainNode<T>* theNode = new ChainNode<T>(element);//创建要插入的节点
ChainNode<T>* p = firstNode;
//寻找插入位置的前驱
for (int i = 0; i < theIndex - 1 ; i ++) {
p = p -> next;
}
theNode -> next = p -> next;
p -> next = theNode;
}
ListSize ++;
}
//链表的成员类iterator
class iterator {
public:
//构造函数
iterator(ChainNode<T>* theNode = NULL) {
node = theNode;
};
//重载运算符
T* operator & () const {
return &(node -> element);
}
T& operator * () const {
return (node -> element);
}
iterator& operator ++ () {//前加 ++ it;
node = node -> next;
return *this;
}
iterator operator ++ (int) {//后加 it ++;
iterator old = *this;
node = node -> next;
return old;
}
bool operator != (const iterator& rightIt) const{
return node != rightIt.node;
}
bool operator == (const iterator& rightIt) const{
return node == rightIt.node;
}
protected:
ChainNode<T>* node;
};
iterator begin() {
return iterator(firstNode);
}
iterator end() {
return iterator(NULL);
}
};
}
#endif /* LinkList_hpp */