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

Copy Method

The code for the Copy method of the LinkedList class is given in Program gif. The Copy method is used to assign the elements of one list to another. It does this by discarding the current list elements and then building a copy of the given linked list.

   program3863
Program: LinkedList class Copy method.

The Copy method begins by calling Purge to make sure that the list to which new contents are being assigned is empty. Then, it traverses the list passed to it one-by-one calling the Append method to append the items to the list begin constructed.

In Section gif the running time for the Append method was determined to be O(1). If the resulting list has n elements, the Append method will be called n times. Therefore, the running time of the Copy method is O(n).


next up previous contents index

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