I have several classes (patient, doc, and treatment) (parents), each class has several types too (temp patient, permanent patient, etc..) (children) I need to create 3 different linked lists, one for each parent type. I have the linked list class working fine, but I'm not sure how I can create several different types of linked lists, where each linked list also has different types of nodes inside it.
The following code isn't really necessary but it's what I've written so far in case I wasn't clear
Linked list class:
#include <iostream>
#include <stdlib.h>
struct node {
int key;
struct node *next;
};
class linked_list {
private:
struct node *head;
struct node *tail;
void insert_beginning(int key) {
if (head->next == nullptr) {
tail = head;
}
struct node *temp;
temp = new struct node;
temp->key = key;
temp->next = head;
head = temp;
}
void insert_end(int key) {
struct node *temp;
temp = new struct node;
temp->key = key;
temp->next = nullptr;
if (head->next == nullptr) {
head->next = temp;
tail = temp;
}
else {
tail->next = temp;
}
tail = temp;
}
void insert_middle(int key) {
struct node *temp;
temp = new struct node;
temp->key = key;
struct node *current = head;
struct node *prev = current;
while (current->key < temp->key) {
prev = current;
current = current->next;
}
prev->next = temp;
temp->next = current;
}
public:
linked_list() {
head = nullptr;
tail = nullptr;
}
void create(int key) {
struct node *temp;
temp = new struct node;
temp->key = key;
temp->next = nullptr;
head = temp;
tail = head;
}
void insert(int key) {
if (head == nullptr) {
cout << "Please create a linked list first using create()\n";
return;
}
if (key < head->key) {
insert_beginning(key);
}
else if ((head->next == nullptr) || (key > tail->key)) {
insert_end(key);
}
else {
insert_middle(key);
}
}
void delete_node(int key) {
if (head == nullptr) {
cout << "List is empty\n";
return;
}
if (head->key == key) {
if (head->next == nullptr) {
delete(head);
head = tail = nullptr;
}
struct node *temp = head;
head = head->next;
delete(temp);
}
else {
struct node *current = head;
struct node *prev = current;
while ((current->key != key) && (current->next != nullptr)) {
prev = current;
current = current->next;
}
if ((current->key != key) && (current->next == nullptr)) {
cout << "Key not found\n";
}
else if ((current->key == key) && (current->next == nullptr)) {
tail = prev;
prev->next = nullptr;
delete(current);
}
else {
prev->next = current->next;
delete(current);
}
}
}
void search_node(int key) {
if (head == nullptr) {
cout << "List is empty\n";
return;
}
if (head->key == key || tail->key == key) {
cout << "Key found\n";
return;
}
struct node *current = head;
while ((current->key != key) && (current->next != nullptr)) {
current = current->next;
}
if (current->key == key) {
cout << "Key found\n";
}
else {
cout << "Key not found\n";
}
}
void print_nodes(void) {
struct node *current = head;
while (current != nullptr) {
cout << current->key << '\n';
current = current->next;
}
}
~linked_list() {
struct node *current = head;
struct node *prev = current;
while (current->next != nullptr) {
current = current->next;
delete(prev);
prev = current;
}
delete(prev);
}
};
Treatment class
#pragma warning(disable:4996)
using namespace std;
#include <iostream>
#include <cstdlib>
#include <cctype>
#include "date.h"
class Treatment {
protected:
int treatment_ID;
int patient_ID;
Date treatment_date;
int treatment_costs;
public:
Treatment() {}
Treatment(int treatment_ID, int patient_ID, int treatment_costs, char treatment_date[11]) {
this->treatment_ID = treatment_ID;
this->patient_ID = patient_ID;
this->treatment_costs = treatment_costs;
this->treatment_date.convert_date(treatment_date);
}
void set_treatment_ID(int treatment_ID) {
this->treatment_ID = treatment_ID;
}
void set_treatment_date(char _date[11]) {
treatment_date.convert_date(_date);
}
void set_patient_ID(int patient_ID) {
this->patient_ID = patient_ID;
}
void set_treatment_costs(int treatment_costs) {
this->treatment_costs = treatment_costs;
}
int get_treatment_ID(void) {
return treatment_ID;
}
int get_patient_ID(void) {
return patient_ID;
}
int get_treatment_costs(void) {
return treatment_costs;
}
};
class Inside_treatment : public Treatment {
private:
Date ending_date;
//class doctor;
int section_num;
public:
Inside_treatment() {}
Inside_treatment(int section_num, char ending_date[11], int treatment_ID, int patient_ID, int treatment_costs, char treatment_date[11]) :Treatment(treatment_ID, patient_ID, treatment_costs, treatment_date) {
this->section_num = section_num;
this->ending_date.convert_date(ending_date);
}
void set_ending_date(char ending_date[11]) {
this->ending_date.convert_date(ending_date);
}
void set_section_num(int section_num) {
this->section_num = section_num;
}
int get_section_num() {
return this->section_num;
}
void print_inside_treatment(void) {
cout << "The section num is " << section_num << "\n";
cout << "The treatment ID is " << treatment_ID << "\n";
cout << "The patient_ID is " << patient_ID << "\n";
cout << "The treatment costs are " << treatment_costs << "\n";
ending_date.print_date();
treatment_date.print_date();
}
};
class Outside_treatment : public Treatment {
private:
int clinic_num;
//class doctor;
public:
Outside_treatment() {}
Outside_treatment(int clinic_num, int treatment_ID, int patient_ID, int treatment_costs, char treatment_date[11]) :Treatment(treatment_ID, patient_ID, treatment_costs, treatment_date) {
this->clinic_num = clinic_num;
}
void set_clinic_num(int clinic_num) {
this->clinic_num = clinic_num;
}
int get_clinic_num() {
return this->clinic_num;
}
};
struct Inside_node {
Inside_treatment t;
Inside_node *next;
};
struct Outside_node {
Outside_treatment t;
Outside_node *next;
};
int main(void) {
char ending_date[11] = "24/12/2017";
char treatment_date[11] = "11/12/2017";
Inside_treatment it(3, ending_date, 490, 11, 25000, treatment_date);
it.print_inside_treatment();
while (1);
};
I'll need to create a linked list for Treatment class, in the same list I'll have nodes of type Inside_Treatment and nodes of type Outside_Treatment, same thing for patients and docs.