Реализация алгоритмов массива записей Tutor в C++: массив фиксированного размера, динамический массив и связанный список

Чтобы добавить алгоритм массива записей преподавателя в C++, вы можете использовать различные методы. Вот несколько различных подходов, которые вы можете рассмотреть:

Метод 1: использование массива фиксированного размера

#include <iostream>
#include <string>
const int MAX_SIZE = 100; // Maximum number of tutor records
struct TutorRecord {
    std::string name;
    int age;
    // Add more fields as needed
};
int main() {
    TutorRecord records[MAX_SIZE]; // Array to store tutor records
    int numRecords = 0; // Counter for the number of records
    // Add a new record
    records[numRecords].name = "John Doe";
    records[numRecords].age = 25;
    numRecords++;
    // Add more records...
    // Print all records
    for (int i = 0; i < numRecords; i++) {
        std::cout << "Record " << i + 1 << ": "
                  << "Name: " << records[i].name << ", "
                  << "Age: " << records[i].age << std::endl;
    }
    return 0;
}

Метод 2. Использование динамического массива (std::vector)

#include <iostream>
#include <string>
#include <vector>
struct TutorRecord {
    std::string name;
    int age;
    // Add more fields as needed
};
int main() {
    std::vector<TutorRecord> records; // Vector to store tutor records
    // Add a new record
    TutorRecord record;
    record.name = "John Doe";
    record.age = 25;
    records.push_back(record);
    // Add more records...
    // Print all records
    for (int i = 0; i < records.size(); i++) {
        std::cout << "Record " << i + 1 << ": "
                  << "Name: " << records[i].name << ", "
                  << "Age: " << records[i].age << std::endl;
    }
    return 0;
}

Метод 3. Использование связанного списка

#include <iostream>
#include <string>
struct TutorRecord {
    std::string name;
    int age;
    // Add more fields as needed
    TutorRecord* next;
};
int main() {
    TutorRecord* head = nullptr; // Head pointer for the linked list
    // Add a new record
    TutorRecord* newRecord = new TutorRecord;
    newRecord->name = "John Doe";
    newRecord->age = 25;
    newRecord->next = nullptr;
    head = newRecord;
    // Add more records...
    // Print all records
    TutorRecord* current = head;
    int recordCount = 1;
    while (current != nullptr) {
        std::cout << "Record " << recordCount << ": "
                  << "Name: " << current->name << ", "
                  << "Age: " << current->age << std::endl;
        current = current->next;
        recordCount++;
    }
// Free memory
    current = head;
    while (current != nullptr) {
        TutorRecord* temp = current;
        current = current->next;
        delete temp;
    }
    return 0;
}