-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp3.cpp
More file actions
347 lines (288 loc) · 9.67 KB
/
Copy pathp3.cpp
File metadata and controls
347 lines (288 loc) · 9.67 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
#include <iostream>
#include <string>
#include "p3.h"
using namespace std;
Person::Person()
{
this->height = 0;
this->weight = 0;
this->nextHeight = nullptr;
this->nextWeight = nullptr;
}
Person::Person(string first, string last, int height, int weight)
{
this->first = first;
this->last = last;
this->height = height;
this->weight = weight;
this->nextHeight = nullptr;
this->nextWeight = nullptr;
}
PersonList::PersonList()
{
this->size = 0;
this->headHeightList = nullptr;
this->headWeightList = nullptr;
}
int PersonList::getSize()
{
return size; // Implictly this->size;
}
void printPerson(Person* person, ostream& os) {
os << person->first << ' ' << person->last << ": height=" << person->height << ", weight=" << person->weight << '\n';
}
/*
PersonList list = // ...
list.printByHeight(std::cout);
ofstream file = // ...
list.printByHeight(file);
*/
void PersonList::printByHeight(ostream &os)
{
Person* current = headHeightList;
while(current != nullptr) {
printPerson(current, os);
current = current->nextHeight;
}
}
void PersonList::printByWeight(ostream &os)
{
Person* current = headWeightList;
while(current != nullptr) {
printPerson(current, os);
current = current->nextWeight;
}
}
// Returns a pointer to a person with matching first and last name, if a person exists in list.
// Otherwise return nullptr
Person* PersonList::findPerson(string first, string last) {
Person* current = headHeightList;
while(current != nullptr) {
if(current->first == first && current->last == last) { //current pointer to the actual person
return current;
}
current = current->nextHeight;
}
return nullptr;
}
bool PersonList::exists(string first, string last)
{
bool exists = findPerson(first, last) != nullptr;
return exists;
}
int PersonList::getHeight(string first, string last) {
Person* searchResult = findPerson(first, last);
if(searchResult!=nullptr) {
return searchResult->height;
}
return -1;
}
int PersonList::getWeight(string first, string last) {
Person* searchResult = findPerson(first, last); // pointer to person allows access to all of the variables within
if(searchResult!=nullptr) {
return searchResult->weight;
}
return -1;
}
// Get pointer to element in height order which precedes a person with a given first and last name
// If no person with the given first and last name exists or if such a person is the first element of the list, return nullptr
Person* PersonList::findPredecessorHeight(string first, string last) {
Person* current = headHeightList;
if (current == nullptr)
return nullptr;
while(current->nextHeight != nullptr) {
Person* next = current->nextHeight;
if(next->first == first && next->last == last) {
return current;
}
current = next;
}
return nullptr;
}
// Get pointer to element in weight order which precedes a person with a given first and last name
// If no person with the given first and last name exists or if such a person is the first element of the list, return nullptr
Person* PersonList::findPredecessorWeight(string first, string last) {
Person* current = headWeightList;
if (current == nullptr)
return nullptr;
while(current->nextWeight != nullptr) {
Person* next = current->nextWeight;
if(next->first == first && next->last == last) {
return current;
}
current = next;
}
return nullptr;
}
// Get pointer to element which in height order would precede a person of a given height and weight if such a person were inserted
// If no such element exists, return nullptr
Person* PersonList::insertionPredecessorHeight(int height, int weight) {
Person* prev = nullptr;
Person* current = headHeightList;
while(current != nullptr) {
if(current->height < height || (current->height == height && current->weight > weight)) {
return prev;
}
prev = current;
current = current->nextHeight;
}
return prev;
}
// Get pointer to element which in weight order would precede a person of a given height and weight if such a person were inserted
// If no such element exists, return nullptr
Person* PersonList::insertionPredecessorWeight(int height, int weight) {
Person* prev = nullptr;
Person* current = headWeightList;
while(current != nullptr) {
if(current->weight > weight || (current->weight == weight && current->height < height)) {
return prev;
}
prev = current;
current = current->nextWeight;
}
return prev;
}
// If a person with the given attributes is not in the list, insert this person into the list, and return true.
// If such a person already is in the list, return false, and do nothing.
bool PersonList::add(string first, string last, int height, int weight)
{
// Check if person is already in the list
if (this->exists(first, last))
{
return false;
}
Person *p = new Person(first, last, height, weight);
// Find elements which will come directly before the person to insert.
// These are the points in the linked list at which we will insert the new person.
Person* heightPredecessor = insertionPredecessorHeight(height, weight);
Person* weightPredecessor = insertionPredecessorWeight(height, weight);
// If no element in the list will come before the new person, this new person will become the head of the list.
if (heightPredecessor == nullptr) {
// The old head of the list now comes direclty after the new person
p->nextHeight = headHeightList;
// Update the new head of the list to be the person which we are adding
headHeightList = p;
}
// Otherwise there will be some element in the list which comes before the new person
else {
// The person which comes after the predecessor will come after the new person since which will be between this person and the predecessor
p->nextHeight = heightPredecessor->nextHeight;
// Update the preceding element so that it's next element is the new person
heightPredecessor->nextHeight = p;
}
if (weightPredecessor == nullptr) {
p->nextWeight = headWeightList;
headWeightList = p;
}
else {
p->nextWeight = weightPredecessor->nextWeight;
weightPredecessor->nextWeight = p;
}
size++;
return true;
}
// Remove a person from the list with the given attributes and return ture or return false if no such person exists.
bool PersonList::remove(string first, string last)
{
// Check if person is in the list
Person* temp = findPerson(first, last);
if (temp == nullptr)
return false;
// Find predecessors of person
Person* heightPredecessor = findPredecessorHeight(first, last);
Person* weightPredecessor = findPredecessorWeight(first, last);
// If the moribund person is the head of list, then after removal, the new head will be the person which comes after the moribund person
if (heightPredecessor == nullptr) {
headHeightList = temp->nextHeight;
}
// Otherwise after the moribund person is removed, the person after the moribund person person becomes the person after the preceding person
else {
heightPredecessor->nextHeight = temp->nextHeight;
}
if (weightPredecessor == nullptr) {
headWeightList = temp->nextWeight;
}
else {
weightPredecessor->nextWeight = temp->nextWeight;
}
delete temp;
size--;
return true;
}
bool PersonList::updateName(string first, string last, string newFirst, string newLast)
{
Person* existPerson = findPerson(first, last);
if(!existPerson) {
return false;
}
existPerson->first = newFirst;
existPerson->last = newLast;
return true;
}
bool PersonList::updateHeight(string first, string last, int height)
{
Person* existPerson = findPerson(first, last);
if(!existPerson) {
return false;
}
else {
int weight = existPerson->weight;
remove(first, last);
add(first, last, height, weight);
return true;
}
}
bool PersonList::updateWeight(string first, string last, int weight)
{
Person* existPerson = findPerson(first, last);
if(!existPerson) {
return false;
}
else {
int height = existPerson->height;
remove(first, last);
add(first, last, height, weight);
return true;
}
}
// Empties contents of a PersonList
void PersonList::clear() {
while(size!=0) {
remove(headHeightList->first, headHeightList->last);
}
}
// Helper function which copies the people from source to the "this" PersonList
void PersonList::copy(const PersonList& source) {
Person* current = source.headHeightList;
while(current != nullptr) {
add(current->first, current->last, current->height, current->weight);
current = current->nextHeight;
}
}
// Cleans up memory used by a PersonList
PersonList::~PersonList()
{
clear();
}
PersonList::PersonList(const PersonList &src)
{
size = 0;
headHeightList = nullptr;
headWeightList = nullptr;
copy(src);
}
// Sets contents of "this" PersonList to be equal to contents of the "src" PersonList
const PersonList &PersonList::operator=(const PersonList &src)
{
// Self-assignment check:
// if a is a PersonList then
// a = a must have no effect
// Without this check, we may inadvertently clear a list.
if (this != &src) {
// Clean up memory which was used by old contents
clear();
// Add new contents
copy(src);
}
return *this;
}