-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.cpp
More file actions
150 lines (130 loc) · 4.62 KB
/
List.cpp
File metadata and controls
150 lines (130 loc) · 4.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
/*
* List.cpp
*
* Class Description: List data collection ADT.
* Class Invariant: Data collection with the following characteristics:
* - Each element is unique (no duplicates).
* - List is sorted by patient's care card number.
* - Each element is sorted into specific section based on care card number
*
* Last modified on: May 2017
* Author: Brendin Green
*/
#include "List.h"
using namespace std;
// Default constructor
List::List() {
// Set up element counts and elements array of pointers
for (int i = 0; i < HASH_SIZE; i++){
this->elementCounts[i] = 0;
this->capacities[i] = this->MAX_ELEMENTS;
this->elements[i] = new Patient[this->MAX_ELEMENTS];
}
}
// Deep Copy Constructor
List::List(const List& listToCopy){
for (int i = 0; i < HASH_SIZE; i++){
this->elementCounts[i] = listToCopy.elementCounts[i];
this->capacities[i] = listToCopy.capacities[i];
this->elements[i] = new Patient[this->capacities[i]];
for (int j = 0; j < this->elementCounts[i]; j++){
this->elements[i][j] = listToCopy.elements[i][j];
}
}
}
// Default deconstructor
List::~List(){
for (int i = 0; i < HASH_SIZE; i++){
delete [] this->elements[i];
this->elements[i] = NULL;
}
}
int List::getSection(const Patient& newElement) const {
return newElement.getCareCard()[0] - '0';
}
// Description: Returns the total element count currently stored in List.
int List::getElementCount() const {
int sum = 0;
for (int i = 0; i < HASH_SIZE; i++) {
sum += this->elementCounts[i];
}
return sum;
}
// Description: Insert an element.
// When "this" List is full, its data structure is expanded to accommodate
// a new element. This is done unbeknown to the client code.
// If the insertion is successful, true is returned otherwise, false is returned.
// Precondition: newElement must not already be in data collection.
// Postcondition: newElement inserted and the appropriate elementCount has been incremented.
bool List::insert(const Patient& newElement){
int section = getSection(newElement);
// Increases size if needed
if (elementCounts[section] == capacities[section]) {
Patient* newArray = new Patient[2*capacities[section]];
for (int i = 0; i < elementCounts[section]; i++) {
newArray[i] = elements[section][i];
}
capacities[section] = capacities[section]*2;
delete [] elements[section];
elements[section] = newArray;
}
// Insert new element
for (int j = 0; j < elementCounts[section]; j++) {
if (elements[section][j] == newElement) {
return false;
}
if (elements[section][j] > newElement) {
for (int k = elementCounts[section]; k > j; k--){
elements[section][k] = elements[section][k-1];
}
elements[section][j] = newElement;
elementCounts[section]++;
return true;
}
}
elements[section][elementCounts[section]] = newElement;
elementCounts[section]++;
return true;
}
// Description: Remove an element.
// If the removal is successful, true is returned otherwise, false is returned.
// Postcondition: toBeRemoved is removed, the appropriate elementCount has been decremented.
bool List::remove( const Patient& toBeRemoved ){
int section = getSection(toBeRemoved);
for (int i = 0; i < elementCounts[section]; i++){
if (elements[section][i] == toBeRemoved){
for (int j = i + 1; j < elementCounts[section]; j++ ) {
elements[section][j-1] = elements[section][j];
}
elementCounts[section]--;
return true;
}
}
return false;
}
// Description: Remove all elements.
void List::removeAll(){
for (int i = 0; i < HASH_SIZE; i++){
elementCounts[i] = 0;
}
}
// Description: Search for target element and returns a pointer to it if found,
// otherwise, returns NULL.
Patient* List::search(const Patient& target){
int section = getSection(target);
for (int i = 0; i < elementCounts[section]; i++) {
elements[section][i].printPatient();
if (elements[section][i] == target){
return &elements[section][i];
}
}
return NULL;
}
// Description: Prints all n elements stored in List in sort order and does so in O(n).
void List::printList() {
for (int i = 0; i < HASH_SIZE; i++) {
for (int j = 0; j < elementCounts[i]; j++) {
elements[i][j].printPatient();
}
}
}