Thanks a lot!!!! :D hi, i am trying out past year questions for a resit and am s
ID: 3647965 • Letter: T
Question
Thanks a lot!!!! :Dhi, i am trying out past year questions for a resit and am stuck with the question below. Please help me filled all the part with /**...**/
Question 1
(a)
In the following you may assume the existence of the ListIterator<e> interface and LinkedList<E> class with the following methods
public interface ListIterator<E>
{
E next();
boolean hasNext();
}
public class LinkedList<E>
{
public void addLast(E obj){..}
public int size(){..}
public ListIterator<E> listIterator(){...}
}
Finished the design of the printBackward method given below using the methods listed above in the ListIterator<E> interface and LinkedList<E> class. You should not introduce any new variable tinto the method. In your answer, do not copy the whole method. Write contents of the Initialisation 1, Initialisation 2, Block 1, Block 2, Block 3 . The printBackward method should be written recursive in a singly list backward. The parameter n specifies the size of the list.
public class MyLinkedList<E> extends LinkedList<E>
{
public void printBackward(int n)
{
if(n > 0)
{
ListIterator<E> itr = /**Initialisation 1**/
int count = /**Initialisation 2**/ 0;
E item;
while(itr.hasNext())
{
/**Block 1**/
}
/**Block 2**/
}else
/**Block 3**/
}
}
(b) Write a simple program that tests the printBackward method designed in part a. As a test list you should use a list of integers: 34, 65, 27, 89 and 12. Your program should print to the screen the following:
myList : 34 65 27 89 12
myList backward: 12 89 27 65 34
Explanation / Answer
sol:-
public void printBackward(int n) {
if (n > ) {
ListIterator(E) itr = listIterator(); /** Initialisation 1 **/
int count = ; /** Initialisation 2 **/
E item;
while (itr.hasNext()) {
/** Block 1 **/
item = itr.next();
if (++count == n) {
System.out.println(item); //prints here
printBackward(n-1);
}
}
/** Block 2 **/
// nothing
} else {
/** Block 3 **/
// nothing
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.