-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpersonList.cpp
65 lines (56 loc) · 1.8 KB
/
personList.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
#include "personList.h"
#include <iostream>
#include <string.h>
using std::cout;
using std::endl;
PersonList::PersonList(){
capacity = 2;
numPeople = 0;
theList = new Person*[capacity];
}
PersonList::~PersonList(){
delete [] theList;
}
void PersonList::addPerson(const char* child_name, const char* father_name, const char* mother_name){
Person *father = 0;
Person *mother = 0;
// try to find the three names in the theList
for(int i = 0; i < numPeople; i++){
if(!strcmp(theList[i]->getName(), child_name)){
cout << "ERROR: " << child_name << " already has parents!!!";
return;
} else if(!strcmp(theList[i]->getName(), father_name)) {
father = theList[i];
} else if(!strcmp(theList[i]->getName(), mother_name)) {
mother = theList[i];
}
}
if(father == 0){
// father_name is not in the theList so create a new person
father = new Person(father_name, 0, 0);
insertIntoList(father);
}
if(mother == 0){
// mother_name is not in the theList so create a new person
mother = new Person(mother_name, 0, 0);
insertIntoList(mother);
}
Person *newChild = new Person(child_name, father, mother);
insertIntoList(newChild);
father->addChild(newChild);
mother->addChild(newChild);
}
void PersonList::insertIntoList(Person *newPerson){
if(numPeople == capacity) expand(&theList, &capacity);
theList[numPeople++] = newPerson;
}
void PersonList::printLineage(const char* person){
for(int i = 0; i < numPeople; i++) {
if(!strcmp(theList[i]->getName(), person)){
theList[i]->printAncestors();
theList[i]->printDecendents();
return;
}
}
cout << endl << person << " is not in the list!" << endl;
}