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

Question 2 You are given a LinkedList<Integer> L}, and another {LinkedList<Integ

ID: 672962 • Letter: Q

Question

Question 2

You are given a LinkedList<Integer> L}, and another {LinkedList<Integer> P}, containing integers sorted in ascending
order. Write a Java method printPositions(LinkedList<Integer> L, LinkedList<Integer> P)} which would
print the elements in L that are in positions specified by P. For instance, if P={1,3,4,6}, the first,
third, fourth and sixth elements in L are printed. You SHOULD use a ListIterator (java.util.ListIterator) to
go through the list and methods of the ListIterator interface when writing your method. See the last
page for the methods available in ListIterator interface.

Explanation / Answer

printPositions(LinkedList<Integer> L, LinkedList<Integer> P){

ListIterator<Integer> litr1 = null;

ListIterator<Integer> litr2 = null;

litr1 =L.listIterator();

litr2 =P.listIterator();

System.out.println("Elements in forward directiton");

int prev = 0;

while(litr2.hasNext()){

int current = litr2.next();
int val ;

while( current > prev && litr1.hasNext()) {
    prev = prev + 1;
    val = litr1.next();
}

System.out.println(val);
       }

  

}

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