2013-05-10 49 views
2

我正在从我的教科书中随机练习题目,并且我想到了这个问题,但无法完成。反向打印一个圆形单链表

如何以相反顺序打印循环单链表?例如:如果列表包含以下元素:1 2 3,应打印它们3 2 1

请注意,它是一个循环链接列表,并且方法中不应有任何参数。

谢谢!

+6

你试过递归吗? – FDinoff 2013-05-10 22:12:02

+0

你有任何我们可以使用的代码吗? – Tdorno 2013-05-10 22:15:07

+0

@Tdorno void printReverse(){ 节点 temp = list; (temp.getNext()== null){ System.out.println(temp.getInfo()); } printReverse(temp.getNext()); } } – user2272227 2013-05-10 22:19:05

回答

3

在基本情况下(开始节点等于下一个节点),打印当前节点。否则,在下一个节点上递归,然后打印当前节点。

请注意,由于堆栈使用线性空间,但由于没有后退指针,因此这是最佳选择。

+0

适用于循环单链表? – user2272227 2013-05-10 22:16:26

+1

将基本情况更改为下一个节点!= head – 2013-05-10 22:16:50

+0

哦,我错过了它是循环的。根据@RonDahlgren编辑。 – 2013-05-10 22:16:53

1

如何:

class Node { 
    int data; 
    Node next; 
    public Node getNode() ... 
    public String toString() ... 
} 

public class CircularList { 

    private Node list; 

    public void printReverse() { 
     final Node head = this.list; 
     printReverseRecurse(list, head); 
     System.out.println(list.toString()); 
    } 

    private void printReverseRecurse(Node node, Node head) { 
     if (node != head) { 
      printReverseRecurse(node.getNext(), head); 
      System.out.print(node.toString()); 
     } 
    } 
} 

编辑后,我忘了通过head参考私有方法。

+1

thnx表示努力,但问题要求在方法中不带参数解决;/ – user2272227 2013-05-10 22:27:48

+0

是的,那对于题。我会编辑。 – Marvo 2013-05-10 22:28:11

+0

final Node head = node; 节点在这里是什么意思?你的意思是名单的开始? – user2272227 2013-05-10 22:32:19

1

下面是在堆上使用堆而不是程序堆栈的非递归方法。应该使用比递归方法更少的内存。

class Node { 
    private int data; 
    private Node next; 
    public Node getNode() { ... } 
    public String toString() { ... } 
} 

public class CircularList { 

    private Node list; 

    public reversePrint() { 
     Stack<Node> stack = new Stack<Node>(); 

     // First, put all the entries in the stack 
     Node node = list; 
     do { 
      stack.push(node); 
      node = node.getNext(); 
     } while (node != list); 

     while (!stack.empty()) { 
      System.out.print(stack.pop()); 
     } 
    } 

} 
+0

有没有其他的选择可以打破?我们不能使用它。 – user2272227 2013-05-10 22:51:56

+0

也是,它为每个方法提供了错误,它说它只能用于数组或迭代。 – user2272227 2013-05-10 22:56:15

+0

对不起。编辑。 – Marvo 2013-05-10 23:20:27

2

下面是一个使用递归的可能解决方案,印刷方法不接受任何参数:

public class ReversePrinter { 

    private Node<?> head; 
    private Node<?> current; 
    private boolean first = true; 

    public ReversePrinter(Node<?> head) { 
     this.head = head; 
     this.current = head; 
    } 

    public void printReverse() { 

     if (current == null) { 
      return; 
     } else if (current == head) { 
      if (!first) return; 
      first = false; 
     } 

     Node<?> previous = current; 
     current = current.getNext(); 
     printReverse(); 
     System.out.println(previous.getInfo()); 

    } 

} 

使用方法如下:

ReversePrinter printer = new ReversePrinter(nodeHeadOfList); 
printer.printReverse(); 

对于这个问题的例子,它将在控制台上打印:

3 
2 
1 
+0

这使我在运行时出现异常。 – user2272227 2013-05-11 08:25:39

+0

用你用来测试它的列表和完整的堆栈跟踪发布代码 – 2013-05-12 11:43:23

+0

顺便说一下:我的答案中的代码已经过测试并且工作正常,如果有例外,这可能是因为您使用的列表未正确构建,开始检查 – 2013-05-12 16:44:08