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

assign Method

The code for the assign method of the LinkedList class is given in Program gif. The assign 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.

   program3836
Program: LinkedList class constructor and assign methods.

The assign 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 assign method is O(n).


next up previous contents index

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