【问题标题】:print backward method for a singly linked list using recursion使用递归打印单链表的向后方法
【发布时间】:2012-08-24 13:19:22
【问题描述】:

我正在尝试过去一年的问题以进行重考,并被以下问题所困扰。 问题一

下面你可以假设存在 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(){...}

}

使用上面列出的 ListIterator 接口和 LinkedList 类中的方法完成了下面给出的 printBackward 方法的设计。您不应该在方法中引入任何新变量。在您的回答中,不要复制整个方法。写入 Initialisation 1, Initialisation 2, Block 1, Block 2, Block 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;
           }
         }
 }

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

【问题讨论】:

  • 如果你只是打印,那么你不需要在链表中添加任何东西。
  • 那么,不能更改 printBackward 方法吗?为什么 ListIterator 会重新初始化?
  • @davidmontoyago,我得到了代码,只允许用 /**...**/ 更改位我不知道为什么要重新初始化...有点帮助? ?

标签: java algorithm recursion iterator linked-list


【解决方案1】:

printBackward 方法设计很诡异,看来他们希望你在每次递归中无论如何都要使用 Iterator 到达最后一个位置,必须是性能/有效性在这里不是问题,否则他们想看看你有多机智。在下面找到解决方案:

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
    }
}

您可以像这样使用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());
}

【讨论】:

  • 谢谢!还有另一种不使用 ++count 的方法吗?并且仅在测试时打印答案,而不是将其构建到 /** Block 1 **??
  • 要避免使用count吗?但它不是包含在方法设计中的变量吗?
  • 我确实想使用count,我想知道是否有另一种方法来实现它,说实话,我不明白++count和count++之间的区别,你能简要介绍一下我?再次感谢!你太有帮助了。 :)
  • @coralbeans 指的是这个stackoverflow.com/questions/1094872/…
【解决方案2】:

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

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

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

}

【讨论】:

  • 这不会使用linkedList吧?我知道如何使用 for 循环来实现它,但需要使用递归单链表来解决这个问题。
【解决方案3】:
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
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 2013-06-02
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 2020-08-10
    • 2014-11-27
    相关资源
    最近更新 更多