0

i am trying out past year questions for a resit and am stuck with the question below. Question 1

In the following you may assume the existence of the ListIterator interface and LinkedList 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 interface and LinkedList 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**/   list1.listIterator();

              int count = /**Initialisation 2**/  0;

              E item;

              while(itr.hasNext())
              {
                /**Block 1**/  addLast(list1); printBackward(); count --;

              }

                /**Block 2**/ E.next;
             }else

             /**Block 3**/ return;
           }
         }
 }

I have inserted my answers next to the /** ..**/ but am unsure if they are correct. It would be much appreciated if someone could help me correct my mistakes

davidmontoyago
  • 1,834
  • 14
  • 18

3 Answers3

0

Get the length of the list and create a for loop to go through them backwards e.g.

for(int i = *sizeOfList*; i > 0; i--)
{

System.out.println(currentItem[i]);

}
Schokea
  • 708
  • 1
  • 9
  • 28
  • This would not be using a linkedList would it? I know how to implement it using a for loop but am required to solve this using a recursive, singly linked list. – coralbeans Aug 24 '12 at 15:07
0

The printBackward method design is very weird, It seems that they want you to use the Iterator no matter what to get to the last position in every recursion, it has to be that the performance/effectiveness is not a concern here or they want to see how witty you are. Find below a solution:

public void printBackward(int n) {

    if (n > 0) {
        ListIterator<E> itr = listIterator(); /** Initialisation 1 **/          
        int count = 0; /** 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
    }
}

You can test it using java.util.LinkedList and java.util.ListIterator like this:

public static void main(String[] args) {
    MyLinkedList<String> list = new MyLinkedList<String>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.printBackward(list.size());
}
davidmontoyago
  • 1,834
  • 14
  • 18
  • Thanks! is there another way to not use ++count? and only to print the answer when testing instead of building it into the /** Block 1 **?? – coralbeans Aug 24 '12 at 16:52
  • Do you want to avoid the use of count? but isn't it a variable included in the method design? – davidmontoyago Aug 24 '12 at 16:58
  • I do want to use count, i was wondering if there is another way to implement it, truth be told, I don't understand the difference between ++count and count++, could you brief it for me? Thanks again! You've been so helpful. :) – coralbeans Aug 24 '12 at 17:19
  • @coralbeans refer to this http://stackoverflow.com/questions/1094872/is-there-a-difference-between-x-and-x-in-java – davidmontoyago Aug 24 '12 at 18:02
0
public void printBackward(int n) {

if (n > 0) {
    ListIterator<E> itr = listIterator(); /** Initialisation 1 **/          
    int count = 0; /** Initialisation 2 **/

    E item;
    while (itr.hasNext()) {
        /** Block 1 **/             
        item = itr.next();
        if (count == n-1) {
            System.out.println(item); //prints here
           count++;
        }               
    }
    /** Block 2 **/
     printBackward(n-1);
} else {            
    /** Block 3 **/
    // nothing
}

}

Echilon
  • 10,064
  • 33
  • 131
  • 217
Vadim
  • 1