-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleLinkedList.cpp
384 lines (361 loc) · 7.08 KB
/
DoubleLinkedList.cpp
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
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* previous;
Node* next;
Node(int d){data=d;previous=NULL;next=NULL;}
};
class NodeList
{
private:
Node* node;
public:
NodeList();
~NodeList();
void clear();
bool empty();
int size();
void print();
void u_print();
int get(int pos);
void insert(int pos, int d);
int remove(int pos);
void push_front(int d);
void push_back(int d);
};
NodeList::NodeList():node(NULL)
{
}
NodeList::~NodeList()
{
clear();
}
//清空链表
void NodeList::clear()
{
cout<<"清空链表"<<endl;
if (node == NULL)
{
return ;
}
else
{
//这里要讲node链表赋NULL,所以要取指针
Node** p = &(node->next);
while (*p != NULL && *p != node)
{
Node* pNext = (*p)->next;
delete *p;
*p = NULL;
*p = pNext;
}
delete node;
node = NULL;
}
}
//判断链表是否为空
bool NodeList::empty()
{
return node == NULL;
}
//返回链表长度
int NodeList::size()
{
if (node == NULL)
{
return 0;
}
else
{
int count = 1;
Node* p = node;
while(p->next != NULL && p->next != node)
{
++count;
p = p->next;
}
return count;
}
}
//打印链表数据
void NodeList::print()
{
cout<<"输出链表:";
if (node == NULL)
{
return ;
}
else
{
Node* p = node;
do
{
cout<<p->data<<" ";
p = p->next;
}while(p != NULL && p != node);
}
cout<<endl;
}
//反向打印链表数据
void NodeList::u_print()
{
cout<<"反向输出链表:";
if (node == NULL)
{
return ;
}
else
{
Node* p = node;
do
{
cout<<p->data<<" ";
p = p->previous;
}while(p != NULL && p != node);
}
cout<<endl;
}
//获取链表位置为pos的数据
int NodeList::get(int pos)
{
cout<<"获取位置为"<<pos<<"的数据 ";
if (pos < 1)
{
cout<<"pos值非法"<<endl;
return 0;
}
if (node == NULL)
{
cout<<"链表为空"<<endl;
return 0;
}
int index = 0;
Node* p = node;
do
{
++index;
if (index == pos)
{
break;
}
else
{
p = p->next;
}
}while(p != NULL && p != node);
if (index < pos)
{
cout<<"链表长度不足"<<pos<<endl;
return 0;
}
cout<<"位置为"<<pos<<"的数据为:"<<p->data<<endl;
return p->data;
}
//位置为pos的链表节点插入数据
void NodeList::insert(int pos, int d)
{
cout<<"位置为"<<pos<<"插入数据"<<d<<" ";
if (pos < 1)
{
cout<<"pos值非法"<<endl;
return ;
}
if (node == NULL)
{
cout<<"链表为空"<<endl;
return ;
}
int size = this->size();
if (size < pos)
{
cout<<"链表长度不足"<<pos<<endl;
return ;
}
else
{
if (size == 1)
{
Node* pNode = new Node(d);
pNode->previous = node;
pNode->next = node;
node->next = pNode;
node->previous = pNode;
node = pNode;
}
else
{
Node* current = node;
int index = 0;
while (current != NULL)
{
++index;
if (index == pos)
{
break;
}
else
{
current = current->next;
}
}
Node* pNode = new Node(d);
pNode->previous = current->previous;
pNode->next = current;
current->previous->next = pNode;
current->previous = pNode;
}
}
}
//移除位置为pos的链表节点
int NodeList::remove(int pos)
{
cout<<"移除位置为"<<pos<<"的数据 ";
if (pos < 1)
{
cout<<"pos值非法"<<endl;
return 0;
}
if (node == NULL)
{
cout<<"链表为空"<<endl;
return 0;
}
int size = this->size();
if (size < pos)
{
cout<<"链表长度不足"<<pos<<endl;
return 0;
}
else
{
int ret = 0;
if (size == 1)
{
ret = node->data;
delete node;
node = NULL;
}
else
{
Node* current = node;
int index = 0;
while (current != NULL)
{
++index;
if (index == pos)
{
break;
}
else
{
current = current->next;
}
}
current->previous->next = current->next;
current->next->previous = current->previous;
ret = current->data;
delete current;
current = NULL;
}
if (size == 1)
{
node->previous = node->next = NULL;
}
return ret;
}
}
//链表头插入数据
void NodeList::push_front(int d)
{
Node* pNode = new Node(d);
if (node == NULL)
{
node = pNode;
return ;
}
else
{
int size = this->size();
if (size == 1)
{
pNode->next = node;
pNode->previous = node;
node->next = pNode;
node->previous = pNode;
}
else
{
pNode->next = node;
pNode->previous = node->previous;
node->previous->next= pNode;
node->previous = pNode;
}
node = pNode;
cout<<"链表头插入"<<pNode->data<<endl;
return ;
}
}
//链表尾插入数据
void NodeList::push_back(int d)
{
Node* pNode = new Node(d);
if (node == NULL)
{
node = pNode;
return ;
}
else
{
int size = this->size();
if (size == 1)
{
pNode->next = node;
pNode->previous = node;
node->next = pNode;
node->previous = pNode;
}
else
{
pNode->next = node;
pNode->previous = node->previous;
node->previous->next= pNode;
node->previous = pNode;
}
cout<<"链表头插入"<<pNode->data<<endl;
return ;
}
}
int main()
{
NodeList* pNodeList = new NodeList;
bool empty = pNodeList->empty();
int size = pNodeList->size();
pNodeList->push_back(3);
pNodeList->print();
pNodeList->u_print();
pNodeList->get(2);
pNodeList->insert(1, 7);
pNodeList->print();
pNodeList->u_print();
pNodeList->get(2);
pNodeList->push_front(5);
pNodeList->print();
pNodeList->u_print();
pNodeList->get(2);
pNodeList->push_front(8);
pNodeList->print();
pNodeList->u_print();
pNodeList->get(2);
pNodeList->remove(2);
pNodeList->print();
pNodeList->u_print();
pNodeList->get(2);
pNodeList->clear();
pNodeList->print();
pNodeList->u_print();
pNodeList->get(2);
system("pause");
return 0;
}