我是否可以仅使用一个链表类创建多个不同节点的链表?

时间:2017-12-10 18:30:00

标签: c++ linked-list

我有几个班级(病人,医生和治疗)(父母),每个班级也有几种类型(临时病人,永久病人等)。(孩子)我需要创建3个不同的链表,一个用于每个父类型。我有链表类工作正常,但我不知道如何创建几种不同类型的链表,其中每个链表也有不同类型的节点。

下面的代码并不是必需的,但这是我到目前为止所写的内容,以防我不清楚

链接列表类:

#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);
    }
};

治疗班

#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);
};

我需要为Treatment类创建一个链表,在同一个列表中我将拥有Inside_Treatment类型的节点和类型为Outside_Treatment的节点,对于患者和文档也是如此。

0 个答案:

没有答案