-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathList.cpp
More file actions
94 lines (83 loc) · 2.64 KB
/
List.cpp
File metadata and controls
94 lines (83 loc) · 2.64 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
/*
* 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.
* - List length will stay equal to or less than MAX_ELEMENTS
*
* Last modified on: May 2017
* Author: Brendin Green
*/
#include <iostream>
#include "List.h"
using namespace std;
// Default constructor
List::List() : elementCount(0), capacity(List::MAX_ELEMENTS) {}
// Description: Returns the total element count currently stored in List.
int List::getElementCount() const {
return elementCount;
}
// Description: Insert an element.
// Precondition: newElement must not already be in data collection.
// Postcondition: newElement inserted and elementCount has been incremented.
bool List::insert(const Patient& newElement){
// Check if appendable
if (elementCount < capacity) {
for (int i = 0; i < getElementCount(); i++) {
if (elements[i] > newElement){
// Insert before ith element
for (int j = getElementCount(); j > i; j--){
elements[j] = elements[j-1];
}
elements[i] = newElement;
elementCount++;
return true;
}
if (elements[i] == newElement) {
return false;
}
}
elements[elementCount] = newElement;
elementCount++;
return true;
}
return false;
}
// Description: Remove an element.
// Postcondition: toBeRemoved is removed and elementCount has been decremented.
bool List::remove(const Patient& toBeRemoved){
// Search for the element we want to remove
for (int i = 0; i < getElementCount(); i++){
if (elements[i] == toBeRemoved){
for (int j = i + 1; j < elementCount; j++ ) {
elements[j-1] = elements[j];
}
elementCount--;
return true;
}
}
return false;
}
// Description: Remove all elements.
void List::removeAll(){
elementCount = 0;
}
// Description: Search for target element.
// Returns a pointer to the element if found,
// otherwise, returns NULL.
Patient* List::search(const Patient& target){
for (int i = 0; i < getElementCount(); i++) {
if (elements[i] == target){
return &elements[i];
}
}
return NULL;
}
// Description: Prints all elements stored in List.
void List::printList() {
for (int i = 0; i < getElementCount(); i++){
elements[i].printPatient();
}
}