Data Structures, Algorithms, & Applications in Java
Chapter 6, Exercise 7
The code to convert an array-based linear list to a chain is
public class ArrayToChain
{
/** convert the array list f to a chain c
* @return Chain version of f */
public static Chain arrayToChain(ArrayLinearList f)
{
Chain c = new Chain();
int s = f.size();
for (int i = s - 1; i >= 0; i--)
// copy i'th element of f
c.add(0, f.get(i));
return c;
}
}
The complexity of
get(i) and
add(0, x) is O(1).
So, the conversion takes
O(size) time, where size
is the length of the list.
The test code is given as the method ArrayToChain.main,
and the output is in the file ArrayToChain.output.