Добавил:
Рад, если кому-то помог Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
0
Добавлен:
01.11.2025
Размер:
3.57 Кб
Скачать
#include <iostream>
#include <locale>
using namespace std;

template<typename T>
class List {
private:
    struct Node {
        T data;
        Node* next;
        Node(const T& d) : data(d), next(nullptr) {}
    };
    
    Node* head;

public:
    List() : head(nullptr) {}
    
    ~List() {
        while (!empty()) {
            removeFromFront();
        }
    }
    
    // Проверка на пустоту
    bool empty() const {
        return head == nullptr;
    }
    
    // Добавление в начало
    void addToFront(const T& data) {
        Node* newNode = new Node(data);
        newNode->next = head;
        head = newNode;
    }
    
    // Добавление в конец
    void addToBack(const T& data) {
        Node* newNode = new Node(data);
        if (empty()) {
            head = newNode;
        } else {
            Node* current = head;
            while (current->next != nullptr) {
                current = current->next;
            }
            current->next = newNode;
        }
    }
    
    // Подсчет вхождений
    int count(const T& data) const {
        int count = 0;
        Node* current = head;
        while (current != nullptr) {
            if (current->data == data) {
                count++;
            }
            current = current->next;
        }
        return count;
    }
    
    // Удаление элемента
    bool remove(const T& data) {
        if (empty()) return false;
        
        if (head->data == data) {
            removeFromFront();
            return true;
        }
        
        Node* current = head;
        while (current->next != nullptr && current->next->data != data) {
            current = current->next;
        }
        
        if (current->next != nullptr) {
            Node* toDelete = current->next;
            current->next = toDelete->next;
            delete toDelete;
            return true;
        }
        
        return false;
    }
    
    // Удаление из начала
    void removeFromFront() {
        if (!empty()) {
            Node* toDelete = head;
            head = head->next;
            delete toDelete;
        }
    }
    
    // Вывод списка
    void print() const {
        Node* current = head;
        cout << "Список: ";
        while (current != nullptr) {
            cout << current->data << " -> ";
            current = current->next;
        }
        cout << "NULL" << endl;
    }
};

int main() {
    setlocale(LC_ALL, "ru_RU.UTF-8");
    
    // Тестирование с целыми числами
    cout << "=== Список целых чисел ===" << endl;
    List<int> intList;
    intList.addToFront(3);
    intList.addToFront(2);
    intList.addToFront(1);
    intList.addToBack(4);
    intList.addToBack(2);
    
    intList.print();
    cout << "Количество двоек: " << intList.count(2) << endl;
    
    intList.remove(2);
    cout << "После удаления первой двойки: ";
    intList.print();
    
    // Тестирование со строками
    cout << "\n=== Список строк ===" << endl;
    List<string> strList;
    strList.addToFront("world");
    strList.addToFront("hello");
    strList.addToBack("!");
    
    strList.print();
    cout << "Количество 'hello': " << strList.count("hello") << endl;
    
    return 0;
}
Соседние файлы в папке Лаба8