Skip to content

Commit 6bac4d6

Browse files
committed
refactored DSDoublyLL<T>::operator=
1 parent 0c06103 commit 6bac4d6

1 file changed

Lines changed: 26 additions & 12 deletions

File tree

DSDoublyLL/dsdoublyll.h

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -714,28 +714,42 @@ T& DSDoublyLL<T>::operator[](int index) const{
714714
return getNodeAt(index)->data;
715715
}
716716

717-
//TODO: may make this faster to replace the data as you go through rather than clear the whole list
718717
/**
719718
* @brief operator = : copy constructor
720719
* @param other - reference to target list
721720
* @return returns this
722721
*/
723722
template <class T>
724723
DSDoublyLL<T>& DSDoublyLL<T>::operator=(const DSDoublyLL<T>& other){
725-
DSNode<T>* next;
726-
DSNode<T>* current = head;
727-
while(current != nullptr){
728-
next = current->next;
729-
delete current;
730-
current = next;
724+
DSNode<T>* dataI = head;
725+
DSNode<T>* otherI = other.head;
726+
727+
while(otherI != nullptr){
728+
if(dataI == nullptr){
729+
break;
730+
} else {
731+
dataI->data = otherI->data;
732+
dataI = dataI->next;
733+
otherI = otherI->next;
734+
}
731735
}
732-
numIndexes = 0;
733736

734-
head = nullptr;
735-
tail = nullptr;
737+
while(otherI != nullptr){
738+
pushBack(otherI->data);
739+
otherI = otherI->next;
740+
}
736741

737-
for(current = other.head; current != nullptr; current = current->next){
738-
pushBack(current->data);
742+
if(dataI != nullptr){
743+
DSNode<T>* next;
744+
tail = dataI->prev;
745+
tail->next = nullptr;
746+
747+
while(dataI != nullptr){
748+
next = dataI->next;
749+
delete dataI;
750+
dataI = next;
751+
numIndexes--;
752+
}
739753
}
740754

741755
return *this;

0 commit comments

Comments
 (0)