I call a function interleave
that looks like
void AddressLinkedList::interleave(AddressLinkedList& other) {
AddressLinkedList temp;
AddressListNode* thisCur = this->head;
AddressListNode* otherCur = other.head;
for (int i = 0; i < this->length + other.length; i++) {
if (i % 2 == 0) {
temp.insertEnd(thisCur->data);
thisCur = thisCur->next;
}
else if (i % 2 != 0) {
temp.insertEnd(otherCur->data);
otherCur = otherCur->next;
}
}
return;
}
This function is supposed to interweave a singly linked list A with a singly linked list B along the lines of if A looked like "1, 2, 3" and B looked like "4, 5, 6", then the call linkedListA.interleave(linkedListB)
should make A "1, 4, 2, 5, 3, 6". I've successfully managed to create a list like this, the problem is it that it's the temp
list, but I don't know how to make it so it would be the this
pointer with an end goal linkedListA
be the aforementioned "singly linked list A". In case it matters, below is the overloaded assignment operator and the insertEnd
function.
void AddressLinkedList::insertEnd(const Address& value) {
if (length == 0) {
this->insertStart(value);
return;
}
AddressListNode* temp = new AddressListNode(value);
length++;
tail->next = temp;
tail = temp;
}
AddressLinkedList& AddressLinkedList::operator=(const AddressLinkedList& other) {
delete this;
AddressListNode* current;
current = other.head;
while (current != nullptr) {
insertEnd(current->data);
current = current->next;
}
return *this;
}