Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

program an array: a of linked lists such that: a[0] is a linked list of one node

ID: 674521 • Letter: P

Question

program an array: a of linked lists such that:

a[0] is a linked list of one node with data value: A.

a[1] is a linked list with two nodes with data values: A, and B respectively.

a[2] is a linked list with three nodes with data values: A, B, and C respectively.

a[3] is a linked list with four nodes with data values: A, B, C, and D respectively.

a[n] is a linked list with n nodes, where n is less than 27 with data values: A, and B, C, …

When we display the content of these n linked lists we should have:

[A]

[A, B]

[A, B, C]

[A, B, C, D]

[A, B, C, D, ….]                                 The first n alphabets, where n < 27

For example if the value of n is 7 we should have an array of 7 linked lists, where:

a[0] would be a linked list with 1 node with data value: A

a[1] would be a linked list with 2 nodes with data values: A, and B.

a[2] would be a linked list with 3 nodes with data values: A, B, and C.

a[3] would be a linked list with 4 nodes with data values: A, B, C, and D.

a[4] would be a linked list with 5 nodes with data values: A, B, C, D, and E.

a[5] would be a linked list with 6 nodes with data values: A, B, C, D, E, and F.

a[6] would be a linked list with 7 nodes with data values: A, B, C, D, E, and F.

import java.util.Arrays;

import java.util.Random;

public class Main{

//This method is complete. Do not change it.

public static void main(String[] args){

    Random r = new Random();

  r.setSeed(System.currentTimeMillis());

  int n = r.nextInt(20) + 1;

  List[]a = new ArrayList[n];

  makeLists(a);

    for(int i = 0; i < a.length; i++)

       System.out.println(a[i].toString());

}

private static void makeLists(List[] a){

    //Complete this method.

}

}

Explanation / Answer

import java.util.*;

import java.util.Random;

public class Main implements ListIterator {

//This method is complete. Do not change it.

public static void main(String[] args){

Random r = new Random();

r.setSeed(System.currentTimeMillis());

int n = r.nextInt(20) + 1;

List[]a = new ArrayList[n];

makeLists(a);

    for(int i = 0; i < a.length; i++)

       System.out.println(a[i].toString());

}

private static void makeLists(List[] a){

ListIterator list = a.listIterator();
      while (listIterator.hasNext()) {
                System.out.println(listIterator.next());
            }
          // for (int i = 0; i < a.size(); i++) {
           //     System.out.println(a.get(i));
           // } by using for loop we will iterate all the elements

   //Complete this method.

}

}