For a list with n nodes what is the maximum number of nodes


class Node
{
int value;
Node next;
Node(int value, Node next)
{
this.value = value;
this.next = next;
}
Node(int value)
{
this.value = value;
}
}
class List {
Node start;
void inverse()
{
Node p = null;
for (Node q = start; q != null; q = q.next)
{
p = new Node(q.value,p);
}
start = p;
}

Please provide answers to the following questions based on above Java classes.

1. For a list with n Nodes, what is the maximum number of nodes that are "live" (i.e., accessible from a "root set" of variables) during the method inverse(), and when does this maximum occur?

2. Give a simple modification of the method inverse() above that minimizes the number of "live" nodes that are necessary for the method to work, so that any item that will not be used later can be immediately reclaimed as "garbage."

3. Suppose there is "heap space" for 10,000 nodes as Java objects and a list of 7,000 nodes which are stored in that heap. When method inverse() with the modification in (2) is applied to that list, at what stages of the computation does garbage collection take place?

Solution Preview :

Prepared by a verified Expert
JAVA Programming: For a list with n nodes what is the maximum number of nodes
Reference No:- TGS01250048

Now Priced at $20 (50% Discount)

Recommended (96%)

Rated (4.8/5)