Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Copy Constructor and Assignment Operator

The code for the copy constructor and assignment operator (operator=) of the LinkedList<T> class is given in Program gif. These functions are similar in that they both build a copy of a given list. The copy constructor first initializes the member variables to represent the empty list. Then, it traverses the referenced list one-by-one, calling the Append function to append the items of the referenced list to the list begin constructed.

   program3683
Program: LinkedList<T> Class Copy Constructor Definition

In Section gif the running time for the Append function was determined to be tex2html_wrap_inline61025. If the resulting list has n elements, the Append function will be called n times. Therefore, the running time of the copy constructor is tex2html_wrap_inline61053. If T is one of the built-in types, tex2html_wrap_inline60973. As a result, the running time of the copy constructor would be T(n)=O(n).

The assignment operator first calls Purge to make sure that the list to which new contents are being assigned is empty. It then builds a copy of the given list in the same way as discussed above for the copy constructor. The running time is equal to that of the Purge function plus that of the copy constructor.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.