Imagine the method below is fully functional. However professor is saying it is not acceptable to use a pointer. I could use a copy constructor instead to accomplish it. Can someone tell me steps to approach a copy constructor needed to allow me to remove the pointer reference from this SortedList SortedList::merge method please?
SortedList* SortedList::merge(const SortedList& other) const
{
//List A
node* a = head;
//Second list B (other)
node* b = other.head;
//Created third doubly linked list
node* x;
//Create a variable to manipulate nodes
node* prev;
//Set the previous node as nullptr to initialize it
prev = nullptr;
//Create an object
SortedList* sort = new SortedList;
//Error handling
if (sort == nullptr)
returnnullptr;
//Traverse in lists a while it is not null
while (a != nullptr && b != nullptr)
{
//Now compare first element of list a against ordered, first element of list b
if (a->num < b->num)
{
//Create list x
x = new node;
//Error handling
if (x == nullptr)
return sort;
//Assign nullptr to x->next (that is the end of list scenario)
x->next = nullptr;
//Error handling. If previous node was not null...
if (prev != nullptr)
//... then assign pointer x to prev->next
prev->next = x;
//if the head is the only element, assign sort.head = x
if (sort->head == nullptr)
sort->head = x;
//
x->prev = prev;
//Assign data value of a to final list x, since a value is less than b value.
x->num = a->num;
//Traverse to next hop on list a
a = a->next;
}
//Else means, vaue of element in list a is greater than value in element list b
else
{
//Initialize node x
x = new node;
if (x == nullptr)
return sort;
x->next = nullptr;
//Error handling, checking if previous node is not null
if (prev != nullptr)
//Then assign prev->next to pointer to point to x element
prev->next = x;
//Error handling checking if object sort is null
if (sort->head == nullptr)
//In that case, value of x is now the head of the sorted list.
sort->head = x;
x->prev = prev;
//Consider value of b to be the final value in order in list x
x->num = b->num;
//Traverse to next hop in the list b
b = b->next;
}
prev = x;
}
while (b != nullptr) {
//if that is the end of a list
x = new node;
if (x == nullptr)
return sort;
x->next = nullptr;
if (prev != nullptr)
prev->next = x;
if (sort->head == nullptr)
sort->head = x;
x->prev = prev;
x->num = b->num;
b = b->next;
prev = x;
}
while (a != nullptr) {
x = new node;
if (x == nullptr)
return sort;
x->next = nullptr;
if (prev != nullptr)
prev->next = x;
if (sort->head == nullptr)
sort->head = x;
x->prev = prev;
x->num = a->num;
a = a->next;
prev = x;
}
//Get the length of the two lists given
sort->count = getLength() + other.getLength();
//Return the final sorted list
return sort;
}
//End****