简体   繁体   English

递归打印单个链表作业

[英]Recursive printing of a singly linked list homework

I have an assignment in my java class that i need to recursively print out a linked list in reverse. 我在java类中有一个作业,需要以递归的方式反向打印出链表。 I have looked online and found numerous examples of recursive methods that do this but take a node in as a parameter, and to my understanding i need to take in a linked list because i need to print the entire list out. 我在网上查看了很多递归方法的示例,这些示例执行此操作,但是将节点作为参数,据我的理解,我需要输入一个链表,因为我需要打印出整个列表。 Below is what code i have written, and it works in the sense that it prints out the list, recursively, but it is still in the same order that i created the list. 下面是我编写的代码,在某种意义上说,它可以递归地打印列表,但它仍然与我创建列表的顺序相同。 After it prints out the list it throws a no such element exception as well. 在打印出列表之后,它也不会引发此类异常。 my main problem/question is wrapping my head around how best to print this recursively. 我的主要问题/问题是如何最好地递归打印。

public void printRecurse2(LinkedList<String> list2)
{
    if(list2 == null)
        return;
    System.out.println(list2.pop());
    printRecurse2(list2);

}

You need to be checking if the node has another next node. 您需要检查该节点是否还有另一个下一个节点。 This way you defer printing each element until you get to the end of the list: 这样,您就可以延迟打印每个元素,直到到达列表的末尾:

void printReverse(Node node) {
    if(node.next != null) { // recurse until the last node is found
        printReverse(node.next);  // print the next node first
    }
    System.out.println(node.data); // print out the node(this is only reached after the last node is found
}

This will print the entire list by passing in the first node of your linked list. 这将通过传递链接列表的第一个节点来打印整个列表。 Then you dont need to pass the entire list to each call. 然后,您无需将整个列表传递给每个呼叫。 You can also use this to print just part of a list by changing which node you pass to the first call. 您还可以通过更改传递给第一个调用的节点来使用它仅打印列表的一部分。

Your list will never be null , this is why the recursion doesn't stop in time. 您的list永远不会为null ,这就是递归不会及时停止的原因。 You need to check whether the list is not empty, instead of for null, eg 您需要检查列表是否不为空,而不是为空,例如

if(list2.peek() == 0)
    return;
System.out.println(list2.pop());
printRecurse2(list2);

This avoids the exception. 这避免了异常。 To print in reverse, you need to start from the other end of the list: 要反向打印,您需要从列表的另一端开始:

if(list2.peekLast() == null)
    return;
System.out.println(list2.pollLast());
printRecurse2(list2);

As this is homework, I'm only going to guide you, not give you the answer. 因为这是家庭作业,所以我仅会指导您,而不是给您答案。

In any recursive function, you need at least one base case to end the recursion. 在任何递归函数中,您至少需要一个基本案例才能结束递归。 You've chosen if (list2 == null) as your base case. 您已选择if (list2 == null)作为基本情况。 At the end of each function call, you're calling printRecurse2(list2) where list2 has had one element popped from it. 在每个函数调用的最后,您要调用printRecurse2(list2) ,其中list2弹出了一个元素。 No matter what, list2 will exist, and not be equal to null . 无论如何, list2将存在,并且不等于null It might , however, be empty . 但是,它可能空的 Consider changing your base case. 考虑更改您的基本情况。

In regards to the order of items removed, consider the Java API for pop() . 关于删除项目的顺序,请考虑Java API for pop() Pop removes the first item from the list. 弹出从列表中删除第一项。 So, the first time you call printRecurse2() , the first item will be popped and printed. 因此,第一次调用printRecurse2() ,第一项将被弹出并打印。 The next time, the item that was second will now be popped and printed. 下次,第二个项目现在将弹出并打印。 You may want to look into another method, like removeLast() . 您可能需要研究另一个方法,例如removeLast()

If you need to get the elements in reverse try using the removeLast() method from the LinkedList API, it will return then remove the last element in the LinkedList : 如果需要反向获取元素,请尝试使用LinkedList API中的removeLast()方法,它将返回然后删除LinkedList中的最后一个元素:

public void printRecurse2(LinkedList<String> list)
{
    if(list.isEmpty()) //base
        return;
    System.out.println(list.removeLast()); //print the last element
    printRecurse2(list); //recurse
}

Additionally, since this method consumes the LinkedList you may want to pass a copy of the LinkedList to this method. 此外,由于此方法消耗了LinkedList您可能希望将LinkedList的副本传递给此方法。

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

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