Поток 1: EXC_BAD_ACCESS (код = 1, адрес = 0x0) cra sh пытается объединить сортировку - PullRequest
0 голосов
/ 23 апреля 2020

Привет, так что на самом деле есть 2 вопроса. Что означают все пути через эту функцию? Также я получаю ошибку сегментации в местах, где я помещаю ошибки. Я попытался отладить, но я запутался, почему это не работает. Если бы кто-то мог не просто сделать это для меня, но и помочь объяснить, что я сделал неправильно, это очень поможет.

  bool operator<=(const Record& rightObj) const {
     return this->k <= rightObj.k;
   }

template <typename T>
class LinkedList : public List<T> {
    protected:
        // represents an element in the linked list
        struct Node {
            T value;
            Node* next;

            Node(T v, Node* n = nullptr)
            : value(v), next(n) { }
        };

        // a pointer to the front of the list
        Node* head;

    private:
        // copy the values from the argument linked list to `this`
        void copy(const LinkedList<T>&);

        // merge sort algorithm
        void mergeSort(Node*&);

        void split(Node*, Node*&, Node*&);

        void merge(Node*&, Node*, Node*);

    public:
        // default constructor
        LinkedList();

        // copy constructor
        LinkedList(const LinkedList<T>&);

        // overloaded assignment operator
        LinkedList<T>& operator=(const LinkedList<T>&);

        // destructor
        virtual ~LinkedList();

        // add the argument to the end of the list
        virtual void append(const T&) override;

        // remove all elements in the list
        virtual void clear() override;

        // return the element at the given position (argument)
        virtual T getElement(int) const override;

        // return the current length of the list
        virtual int getLength() const override;

        // insert the given element (argument 2) at
        // the given position (argument 1)
        virtual void insert(int, const T&) override;

        // determine if the list currently empty
        virtual bool isEmpty() const override;

        // print the elements in the list starting at the given Node (argument)
        void print(Node*) const;

        // remove the element at the given position (argument)
        virtual void remove(int) override;

        // replace the element at the given position (argument 1) with
        // the value given (argument 2)
        virtual void replace(int, const T&) override;

        // sort the elements in the list using the algorithm (argument)
        //      4 --> merge sort
        virtual void sort(int) override;
};


template <typename T>
void LinkedList<T>::mergeSort(Node*& curr) {
    Node* n = curr;
    Node* a;
    Node* b;

    if (n == nullptr || n->next == nullptr) {
        return;
    } // if

// splits linked list
    split(n, a, b);

// recursive calls for sorting
    mergeSort(a);
    mergeSort(b);

// merging lists
    merge(n, a, b);
}  


template <typename T>
void LinkedList<T>::merge(Node*& curr, Node* A, Node* B) { //I get "All paths through this function will call itself here"
    // Node* result = nullptr;

    if (A == nullptr) {
        curr = B;
    } // if
    else if (B == nullptr) {
        curr = A;
    } // else if

    if (A->value <= B->value) { //Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
        // cout << "TEST" << endl;
        // result = A;
        cout << "TEST" << endl;
        merge(curr, A->next, B); // Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
        cout << "TEST" << endl;
    } // if
    else {
        // cout << "TEST" << endl;
        // result = B;
        merge(curr, A, B->next); // segmenation fault here
    } // else

    curr = A;
}
...