File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 */
723722template <class T >
724723DSDoublyLL<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 ;
You can’t perform that action at this time.
0 commit comments