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

Given two sorted lists , L1 and L2 , complete a following procedure in Java to c

ID: 3910480 • Letter: G

Question

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

a)

int size=list1.size()>list2.size()?list1.size():list2.size();
int a=itr1.next();
int b=itr2.next();
List<Integer> diff=new ArrayList<>();
for (int i = 0; i < size; i++) {
if(a==b) {
a=itr1.next();
b=itr2.next();
}
else if (a>b)
{
diff.add(b);
b=itr2.next();
}else if(i==size-1)
{
diff.add(a);
diff.add(b);
}
else
{
diff.add(a);
a=itr1.next();
}

}
System.out.println(diff);

b)

int size=list1.size()>list2.size()?list1.size():list2.size();
int a=itr1.next();
int b=itr2.next();
List<Integer> intersec=new ArrayList<>();
for (int i = 0; i < size; i++) {
if(a==b) {
intersec.add(x);
a=itr1.next();
b=itr2.next();
}
else if(a>b) {
b=itr2.next();
}else {
a=itr1.next();
}

}

System.out.println(intersec);

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