使用递归打印单个链表的后向方法

时间:2021-09-03 07:17:49

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

我正在尝试过去一年的问题,我会坚持下面的问题。问题1

In the following you may assume the existence of the ListIterator interface and LinkedList class with the following methods

在下文中,您可以使用以下方法假设存在ListIterator接口和LinkedList类

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.

使用上面ListIterator接口和LinkedList类中列出的方法完成下面给出的printBackward方法的设计。您不应该在方法中引入任何新变量。在你的回答中,不要复制整个方法。写入初始化1,初始化2,块1,块2,块3的内容。 printBackward方法应该在单个列表中向后递归写入。参数n指定列表的大小。

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

我在/ ** .. ** /旁边插入了我的答案,但我不确定它们是否正确。如果有人能帮助我纠正错误,我将不胜感激

3 个解决方案

#1


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:

printBackward方法设计非常奇怪,似乎他们希望你使用迭代器而不管在每次递归中到达最后一个位置,它必须是性能/效果在这里不是一个问题,或者他们想要看到你有多诙谐。在下面找到解决方案:

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:

你可以使用java.util.LinkedList和java.util.ListIterator来测试它,如下所示:

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());
}

#2


0  

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

获取列表的长度并创建一个for循环以向后浏览它们,例如

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

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

}

#3


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
}

}

#1


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:

printBackward方法设计非常奇怪,似乎他们希望你使用迭代器而不管在每次递归中到达最后一个位置,它必须是性能/效果在这里不是一个问题,或者他们想要看到你有多诙谐。在下面找到解决方案:

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:

你可以使用java.util.LinkedList和java.util.ListIterator来测试它,如下所示:

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());
}

#2


0  

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

获取列表的长度并创建一个for循环以向后浏览它们,例如

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

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

}

#3


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
}

}