Hi so there are actually 2 questions. What does All paths through this function will call itself mean? Also I get a segmation fault at the places I put the errors.I tried debugging but I'm confused why it's not working. If someone could not just do this for me but also help explain what I did wrong that'll help alot.
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;
}
Aucun commentaire:
Enregistrer un commentaire