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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class NodeType
{
private:
int value;
int doctorID;
string first;
string last;
NodeType *next;
friend class Patient;
};
class Patient
{
private:
NodeType *head;
public:
Patient();
bool empty();
void addValue(int, int, string, string);
void addFront(int, int, string, string);
void removeValue(int);
void print();
~Patient();
};
Patient::Patient()
{
head = NULL;
}
bool Patient::empty()
{
if (head == NULL)
{
return true;
}
else false;
}
void Patient::addValue(int val, int dID, string F, string L)
{
NodeType *ptr = new NodeType;
ptr->value = val;
ptr->doctorID = dID;
ptr->first = F;
ptr->last = L;
ptr->next = NULL;
//if list is empty
if (head == NULL)
{
head = ptr; //head points to ptr
}
else
{
NodeType *current = head; //to traverse
while (current->next != NULL)
{
current = current->next;
}
//reaches the end of the list
current->next = ptr; //how current next points to ptr, ptr next points to null
}
ptr = NULL;
}
void Patient::addFront(int val, int dID, string F, string L)
{
NodeType *ptr = new NodeType;
ptr->value = val;
ptr->doctorID = dID;
ptr->first = F;
ptr->last = L;
ptr->next = NULL;
if (empty())
{
head = ptr;
}
else
{
head = ptr;
ptr->next = head->next; //ptr next points to the next node from head
}
ptr = NULL;
}
void Patient::print()
{
if (head == NULL)
{
cout << "List is empty, nothing to print!" << endl;
}
else
{
NodeType *current = head;
while (current)
{
cout << current->first << " " << current->last << endl;
cout << current->value << " " << current->doctorID << endl;
for (int i = 0; i < 30; i++)
{
cout << "*";
}
cout << endl;
current = current->next;
}
}
}
Patient::~Patient()
{
NodeType *current = head;
while (current)
{
NodeType *garbage = current;
current = current->next;
delete garbage;
}
current = NULL;
head = 0;
}
|