Data Structures, Algorithms, & Applications in Java
Chapter 5, Exercise 12
The method to clone a linear list is given below. This method also
clones the individual elements of the linear list.
public Object clone()
{// overrides Object.clone()
ArrayLinearListWithClone x =
new ArrayLinearListWithClone();
x.size = size;
x.element = new Object [element.length];
for (int i = 0; i < size; i++)
x.element[i] = (element[i] == null) ? null :
((CloneableObject) element[i]).clone();
return x;
}
The time needed to create the array x.element
is O(element.length), because
each element of x.element gets initialized
to null. To determine the complexity of
the for loop,
assume that each element of the list can be cloned in
O(1) time. Under this assumption
The complexity of the
for loop is
O(size). Since
size <= element.length, the overall complexity is
O(element.length).
A test program and output appear in the files
ArrayLinearListWithClone.*.