java 以相反顺序打印链表的递归方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7855820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursive method that prints a linked list in reverse order
提问by user1006161
I'm having trouble writing a method that should accept a reference to a generic single-linked list and creating a test program to testing my method on a list of strings (print the list both in-order and reverse-order). I have already created a single-linked list called SinglyLinkedList.
我在编写一个方法时遇到了问题,该方法应该接受对通用单链表的引用,并创建一个测试程序来在字符串列表上测试我的方法(按顺序和逆序打印列表)。我已经创建了一个名为 SinglyLinkedList 的单链表。
回答by corsiKa
Well if you think about recursion, you know you're going to be doing something over and over again. In this case we want to print a node over and over, but we want a different node each time.
好吧,如果您考虑递归,您就会知道自己会一遍又一遍地做某事。在这种情况下,我们想一遍又一遍地打印一个节点,但我们每次都想要一个不同的节点。
Our first node we print should be the last in the list. That indicates to me an excellent base case.
我们打印的第一个节点应该是列表中的最后一个。这对我来说是一个很好的基本案例。
void printReverse(Node node) {
if(node.next != null) { // we recurse every time unless we're on the last one
printReverse(node.next); // this says "do this to the next node first"
}
System.out.println(node.data); // we'll print out our node now
}
Consider if you had
考虑一下你是否有
1,2,3,4
1,2,3,4
You'd call print on the node with 1 in it. It would then say "I have a next node, print that". The 2 node also has a next node, so it defers to node 3. Node 3 still has a next node, so it defers to node 4 before printing. Node 4 is willing to print itself since it has no next node. Then it returns to where node 3 left off. Now node 3 can print and go back to where node 2 left off. It prints and goes to where node 1 left off. It prints and returns to the main function.
您可以在其中包含 1 的节点上调用 print 。然后它会说“我有一个下一个节点,打印那个”。2节点还有一个下一个节点,所以它服从节点3。节点3还有一个下一个节点,所以它在打印之前服从节点4。节点 4 愿意打印自己,因为它没有下一个节点。然后它返回到节点 3 停止的地方。现在节点 3 可以打印并返回到节点 2 停止的地方。它打印并转到节点 1 停止的地方。它打印并返回到主函数。
回答by Luchian Grigore
For in-order, call the output method before calling the function recurisvely.
对于有序,在递归调用函数之前调用输出方法。
void print(Node n)
{
if ( n != null )
{
System.out.println(n.value);
print(n.next);
}
}
For reverse, call the function first and the output.
对于反向,首先调用函数和输出。
void print(Node n)
{
if ( n != null )
{
print(n.next);
System.out.println(n.value);
}
}
回答by Chris
I agree 100% with the answers above, for my implementation I could not get this to work without a helper class. My answer is only to expand on the answers above, in case others get compilation errors too.
我 100% 同意上面的答案,对于我的实现,如果没有帮助类,我无法让它工作。我的答案只是扩展上面的答案,以防其他人也遇到编译错误。
public void printReverse()
{
printReverse(head);
}
private void printReverse(GNode<T> node)
{
if (node.next != null)
{
printReverse(node.next);
}
System.out.println(node.data);
}