Двусв'язный список.Краш деструктора

Пробую реализовать двусв'язный список,но возникла проблема.Как я понял что деструктор при удалении обьекта крашиться,может кто подсказать в чем причина? Вот код:

List.h

#pragma once
#include <iostream>

template <typename T>
class List{
private:
    template <typename T>
    class Node {
    public:
        T data;
        Node* ptrNext;
        Node* ptrPrev;
    };
    int SIZE;
    Node<T>* head;
    Node<T>* tail;
public:
    List() :head(nullptr), tail(nullptr) {};
    ~List();
    
    int get_size() { return this->SIZE; }

    void push_back(T);
    void pop_front();
    void clear();

    T& operator[](const int);
};

List.cpp

#include "List.h"

template <typename T>
List<T>::~List() {
    clear();
}

template <typename T>
void List<T>::clear() {
    while (SIZE) {
        pop_front();
    }
}

template <typename T>
void List<T>::pop_front() {
    if (SIZE == 0) {
        std::cout << "List is empty" << std::endl;
    }
    else if (SIZE == 1) {
        delete head;
        SIZE--;
    }
    else {
        Node<T> *tmp = head;
        head = head->ptrNext;
        delete tmp;
        SIZE--;
    }
}


template <typename T>
void List<T>::push_back(T data) {
    Node<T>* tmp = new Node<T>;
    tmp->ptrNext = nullptr;
    tmp->data = data;

    if (head != nullptr) {
        tmp->ptrPrev = tail;
        tail->ptrNext = tmp;
        tail = tmp;
    }
    else {
        tmp->ptrNext = nullptr;
        head = tail = tmp;
    }
    SIZE++;
}

template <typename T>
T& List<T>::operator[](const int index) {
    int counter = 0;
    Node<T>* tl = tail;
    int check = (get_size() - 1) - index;
    if (index < check) {
        Node<T>* current = head;
        while (current != nullptr) {
            if (counter == index) {
                return current->data;
            }
            current = current->ptrNext;
            counter++;
        }
    }
    else {
        counter = get_size() - 1;
        Node<T>* current = tail;
        while (current != nullptr) {
            if (counter == index) {
                return current->data;
            }
            current = current->ptrPrev;
            counter--;
        }
    }
}

main.cpp

#include <iostream>
#include "List.h"
#include "List.cpp"

template <typename T>
void print(List<T> lst) {
    for (int i = 0; i < lst.get_size(); i++) {
        std::cout << lst[i] << " ";
    }
    std::cout << std::endl;
}

int main() {
    List<int> lst;
    lst.push_back(5);
    lst.push_back(5);
    lst.push_back(5);
    lst.push_back(5);
    lst.push_back(8);
    lst.push_back(12);
    lst.push_back(36);
    print(lst);
    return 0;
}

Буду очень благодарен


Ответы (0 шт):