简体   繁体   English

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

[英]print backward method for a singly linked list using recursion

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. 使用ListIterator接口和LinkedList类中上面列出的方法,完成了下面给出的printBackward方法的设计。 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 . 写入初始化1,初始化2,块1,块2,块3的内容。 The printBackward method should be written recursive in a singly list backward. 应该将printBackward方法以递归形式写在单个反向列表中。 The parameter n specifies the size of the list. 参数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 这将不胜感激,如果有人可以帮助我纠正我的错误

Get the length of the list and create a for loop to go through them backwards eg 获取列表的长度,并创建一个for循环以向后遍历它们,例如

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

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

}

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. printBackward方法的设计非常奇怪,似乎他们希望您使用Iterator而不管每次递归中要到达最后位置如何,都必须在这里不考虑性能/有效性,否则他们希望看到你真机智 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: 您可以使用java.util.LinkedListjava.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());
}
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
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM