-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW1.cpp
More file actions
350 lines (286 loc) · 7.62 KB
/
Copy pathHW1.cpp
File metadata and controls
350 lines (286 loc) · 7.62 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
347
348
349
350
//HW1 by Jillian Handrahan
//SUNetID: jehandra SUID: 8377009572
#include <iostream>
using namespace std;
class Dnode {
public:
int value;
Dnode* next;
Dnode* previous;
Dnode(int i) { value = i; next = previous = nullptr; }
Dnode() { next = previous = nullptr; }
};
class DLL {//Doubly Linked List
public:
Dnode* head;
Dnode* tail;
DLL(int n, int m);//Constructor; Construct an n-node DLL with random
//values in 0 ... m-1
DLL() { head = tail = nullptr; }
void PrintF();//Print forward from head
void PrintB();//Print backward from tail
void RemoveAll(int k);//Delete all nodes with value k.
void Reverse();
void Sort1();
void Sort2();
void Swap(Dnode* p1, Dnode* p2);
/*
Swap the two nodes pointed by p1 and p2, respectively.
You are only allowed to change the "next" and "previous" of each node, but not "value".
Explanation will be given in class.
You can assume p1 is on the left side of p2. Note that, in a DLL, we assume "head" is on the left side and "tail" is
on the right side.
*/
};
void DLL::RemoveAll(int k) {
if (!head) return;//empty
Dnode* p1{ head };
while (p1) {//while p1 is not nullptr
Dnode* p2 = p1->next;
if (p1->value == k) {
if (p1 == head && p1 == tail) {
head = tail = nullptr;
delete p1;
return;
}
else if (p1 == head) {
head = p1->next;
head->previous = nullptr;
delete p1;
}
else if (p1 == tail) {
tail = p1->previous;
tail->next = nullptr;
delete p1;
}
else {
p1->previous->next = p1->next;
p1->next->previous = p1->previous;
delete p1;
}
}
p1 = p2;
}
}
void DLL::Sort1() {
if (!head || !head->next) return;//0 or 1 node
Dnode* p1{ head };
while (p1->next) {
Dnode* p2{ p1->next }, * p3{ p1->next };
Dnode* pmin{ p1 };
int min{ p1->value };
while (p2) {//while p2 is not nullptr
if (p2->value < min) {
min = p2->value;
pmin = p2;
}
p2 = p2->next;
}
if (pmin != p1) {//swap pmin->value with p1->value
pmin->value = p1->value;
p1->value = min;
}
//p1 = p1->next;
p1 = p3;
}
}
void DLL::Swap(Dnode* p1, Dnode* p2) {
//case 1: head/tail (change 4)
//case 2: next to each other (change 6)
//case 3: general (change 8)
//do not touch value
//good
if (head == nullptr || head->next == nullptr) {
return;
}
//good
if (p1 == head && p2 == tail && p1->next == p2) {
Dnode* ph{ p1 };
p1->next = nullptr;
p1->previous = p2;
p2->previous = nullptr;
p2->next = ph;
head = p2;
tail = p1;
//PrintF();
return;
}
//seems to work, looks ok
if (p1 == head && p2 == tail) {
p2->next = p1->next;
p2->next->previous = p2;
p1->previous = p2->previous;
p1->previous->next = p1;
p2->previous = nullptr;
p1->next = nullptr;
head = p2;
tail = p1;
//PrintF();
return;
}
//should be good looks weird
if (p1 != head && p2 == tail && p1->next == p2) {
Dnode* pt{ p1->next };
p1->next = p2->next;
p2->next = pt;
p2->next->previous = p2;
pt = p1->previous;
p1->previous = p2->previous;
p2->previous = pt;
p1->previous->next = p1;
p2->previous->next = p2;
tail = p1;
// PrintF();
return;
}
//looks verified, maybe weird tail behavior
if (p1 != head && p2 == tail) {
Dnode* ph{ p1->next };
p1->next = p2->next;
p2->next = ph;
p2->next->previous = p2;
ph = p1->previous;
p1->previous = p2->previous;
p2->previous = ph;
p1->previous->next = p1;
p2->previous->next = p2;
tail = p1;
//PrintF();
return;
}
//verified
if (p1 == head && p2 != tail && p1->next == p2) {
Dnode* ph{ p1->next };
// move head to p2
p1->next = p2->next;
p2->next = ph;
p1->next->previous = p1;
p2->next->previous = p2;
head = p2;
ph = p1->previous;
p1->previous = p2->previous;
p2->previous = ph;
p1->previous->next = p1;
//PrintF();
return;
}
//verified
if (p1 == head && p2 != tail) {
Dnode* ph{ p1->next };
// move head to p2
p1->next = p2->next;
p2->next = ph;
p1->next->previous = p1;
p2->next->previous = p2;
p1->previous = p2->previous;
p2->previous = nullptr;
head = p2;
//put p2 in head
p1->previous->next = p1;
//PrintF();
return;
}
//verified
if (p1->next == p2) {
Dnode* pd{ p1->next};
p1->next = p2->next;
p2->next = pd;
p1->next->previous = p1;
p2->next->previous = p2;
pd = p1->previous;
p1->previous = p2->previous;
p2->previous = pd;
p1->previous->next = p1;
p2->previous->next = p2;
//PrintF();
return;
}
//verified
if (p1 != head && p2 != tail) {
Dnode* pp{ p1->next };
p1->next = p2->next;
p2->next = pp;
p1->next->previous = p1;
p2->next->previous = p2;
pp = p1->previous;
p1->previous = p2->previous;
p2->previous = pp;
p1->previous->next = p1;
p2->previous->next = p2;
//PrintF();
return;
}
}
void DLL::Sort2() {
if (!head || !head->next) return;//0 or 1 node
Dnode* p1{ head };
while (p1->next) {
Dnode* p2{ p1->next }, *p3{p1->next};
Dnode* pmin{ p1 };
int min{ p1->value };
while (p2) {//while p2 is not nullptr
if (p2->value < min) {
min = p2->value;
pmin = p2;
}
p2 = p2->next;
}
if (pmin != p1) {//swap pmin->value with p1->value
//pmin->value = p1->value;
//p1->value = min;
//PrintF();
//cout << "aaa" << endl;
Swap(p1, pmin);
}
//p1 = p1->next;
p1 = p3;
}
}
DLL::DLL(int n, int m) {
head = tail = nullptr;
for (int i = 0; i < n; ++i) {
Dnode* p1{ new Dnode{rand() % m} };
if (!head) {//empty
head = tail = p1;
}
else {
tail->next = p1;
p1->previous = tail;
tail = p1;
}
}
}
void DLL::PrintF() {
cout << endl;
Dnode* p1{ head };
while (p1) {
cout << p1->value << " ";
p1 = p1->next;
}
}
void DLL::PrintB() {
cout << endl;
Dnode* p1{ tail };
while (p1) {
cout << p1->value << " ";
p1 = p1->previous;
}
}
int main() {
// DLL L1{ 30, 20 };
// L1.PrintF();
// L1.PrintB();
// L1.RemoveAll(1);
// L1.PrintF();
// L1.PrintB();
// L1.Sort1();
// L1.PrintF();
// L1.PrintB();
DLL L2{ 150, 10 };
L2.PrintF();
// L2.PrintB();
L2.Sort2();
L2.PrintF();
// L2.PrintB();
return 0;
}