The first thing is to recognize that copyList(), the copy constructor. and the assignment operator are all very similar. You can implement the last 2 in terms of the first:
template <class Type>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
doublyLinkedList<Type>::doublyLinkedList(const doublyLinkedList<Type>& otherLis\
t)
{
// initialize this list to be empty.
first = last = nullptr;
count = 0;
// Now copy otherList into it.
copyList(otherList);
}
template <class Type>
const doublyLinkedList<Type>& doublyLinkedList<Type>::operator=(const doublyLin\
kedList<Type> &rhs)
{
copyList(rhs); // copy rhs to this list.
return *this; // return a reference to this list.
}
Now you just have to write copyList. Here is some pseudo-code:
1 2 3 4 5 6 7 8 9 10 11 12
template <class Type>
void doublyLinkedList<Type>::copyList(const doublyLinkedList<Type>& otherList)
{
// Clear out the current contents of this list.
// for each item in otherList
// append it to this list
// I see this is a sorted list. In the loop above, go through otherList
// backwards (from last to first) and call insert(). That way each new
// item will be inserted at the front of the list, which means it will
// run fast.
}