Hey guys! I am working on a project using single linked list and currently on the last task which is overloading the += operator. My merge from a+=b is inefficient because I am having a memory link problem and its slow when it used in list size of 100000. Please let me know if you guys see a error. Thanks for all the help!
//overloaded plus equal (+=) operator
OList & operator += (const OList & other) {
Node<T> *temp,
*p1 = head, //list on the left of operation a+=b
* p2 = other.head; //list on the right
size += other.size; //change size of current list to list suze += right list size
//data hold the data for the Node and next points to next node
while ( p2->next != NULL && p1->next != NULL ) {
if (p1->next->data <= p2->next->data) {
p1= p1->next;
}
else{temp = createNode(p2->next->data);
temp->next = p1->next;
p1->next = temp;
p1 = p1->next;
p2 = p2->next;
}
}
while (p1->next != NULL) {
p1 = p1->next;
}
while (p2->next != NULL) {
p1->next = createNode(p2->next->data);
p1 = p1->next;
p2 = p2->next;
if (p1->next == NULL) lastNode = p1;
}
lastNode = p1;
return *this;
}