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

2. Given two sorted lists , L1 and L2 , complete a following procedure in Java t

ID: 3898679 • Letter: 2

Question

2. Given two sorted lists, L1 and L2, complete a following procedure in Java to compute:

(a) L1 L2 = { x | x Î L1 and x Ï L2 } using only the basic list operators (next(), hasNext(), and compareTo()) and one loop.

public static <AnyType extends Comparable<? super AnyType>>

      void difference(List<AnyType> L1, List<AnyType> L2, List<AnyType> Difference)

{

    ListIterator<AnyType> iterL1 = L1.listIterator();

    ListIterator<AnyType> iterL2 = L2.listIterator();   

    if ( iterL1.hasNext() && iterL2.hasNext() )

    {

      itemL1 = iterL1.next();

      itemL2 = iterL2.next();

    }

    // YOUR CODE GOES HERE

   

               

(b) L1 Ç L2 = { x | x Î L1 and x Î L2 } using only the basic list operators (next(), hasNext(), and compareTo()) and one loop.

public static <AnyType extends Comparable<? super AnyType>>

void intersection(List<AnyType> L1, List<AnyType> L2, List<AnyType> Intersect)

{

    ListIterator<AnyType> iterL1 = L1.listIterator();

    ListIterator<AnyType> iterL2 = L2.listIterator();   

    if ( iterL1.hasNext() && iterL2.hasNext() )

    {

      itemL1 = iterL1.next();

      itemL2 = iterL2.next();

    }

    // YOUR CODE GOES HERE

Explanation / Answer

If you have any doubts, please give me comment...

public static <AnyType extends Comparable<? super AnyType>> void difference(List<AnyType> L1, List<AnyType> L2,

List<AnyType> Difference) {

ListIterator<AnyType> iterL1 = L1.listIterator();

ListIterator<AnyType> iterL2 = L2.listIterator();

AnyType itemL1 = null, itemL2 = null;

if (iterL1.hasNext() && iterL2.hasNext()) {

itemL1 = iterL1.next();

itemL2 = iterL2.next();

}

while (itemL1 != null) {

int compareResult = itemL1.compareTo(itemL2);

if (compareResult != 0) {

Diference.add(itemL1);

itemL1 = iterL1.hasNext() ? iterL1.next() : null;

itemL2 = iterL2.hasNext() ? iterL2.next() : null;

} else if (compareResult = 0) {

itemL1 = iterL1.hasNext() ? iterL1.next() : null;

}

}

}

public static <AnyType extends Comparable<? super AnyType>> void intersection(List<AnyType> L1, List<AnyType> L2,

List<AnyType> Intersect) {

ListIterator<AnyType> iterL1 = L1.listIterator();

ListIterator<AnyType> iterL2 = L2.listIterator();

AnyType itemL1 = null, itemL2 = null; // get first item in each list

if (iterL1.hasNext() && iterL2.hasNext()) {

itemL1 = iterL1.next();

itemL2 = iterL2.next();

}

while (itemL1 != null && itemL2 != null) {

int compareResult = itemL1.compareTo(itemL2);

if (compareResult == 0) {

Intersect.add(itemL1);

itemL1 = iterL1.hasNext() ? iterL1.next() : null;

itemL2 = iterL2.hasNext() ? iterL2.next() : null;

} else if (compareResult < 0) {

itemL1 = iterL1.hasNext() ? iterL1.next() : null;

} else {

itemL2 = iterL2.hasNext() ? iterL2.next() : null;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote