Recursively add a node at a certain index on Linked List

拟墨画扇 提交于 2020-01-06 07:28:08

问题


I'm trying to add a list node at a specified index recursively. By that I mean the List class addAtRec() calls addAtRec() in the ListNode class, that method is supposed to be recursive.

This is what I did:

List:

public class List implements Cloneable {

private ListNode firstNode;
private ListNode lastNode;
private String name;
private int counter;

public List(){
    this("list");
}
public void addAtRec(Object obj, int k)
{
    if(firstNode != null)
        firstNode.addAtRec(obj, k, firstNode);
}
}

That's of course only the relevant parts...

ListNode:

public class ListNode implements Cloneable {

Object data;
ListNode nextNode;
public ListNode(Object o){
    this(o,null);
}
public ListNode(Object o,ListNode node){
    data=o;
    nextNode=node;
}
public void addAtRec(Object obj, int k, ListNode current) throws ListIndexOutOfBoundsException {
    if(current.nextNode == null && k != 0)
        throw new ListIndexOutOfBoundsException(); //line 47
    if(k == 0)
    {
        ListNode l = new ListNode(obj);
        l.nextNode = current.nextNode;
        current.nextNode = l;
    }
    current = current.nextNode;
    addAtRec(obj, k, current); //line 55
    k--;
}

ListIndexOutOfBoundsException:

public class ListIndexOutOfBoundsException extends RuntimeException {

}

my main() method:

String s1 = "Objects";
    String s2 = "to";
    String s3 = "check";
    String s4 = "are";
    String s5 = "strings";
    List list = new List("My list");
    list.insertAtBack(s1);
    list.insertAtBack(s2);
    list.insertAtBack(s3);
    list.insertAtBack(s4);
    list.insertAtBack(s5);

    list.addAtRec(s3, 2);

and the error:

Exception in thread "main" ListIndexOutOfBoundsException
at ListNode.addAtRec(ListNode.java:47)
at ListNode.addAtRec(ListNode.java:55)
at ListNode.addAtRec(ListNode.java:55)
at ListNode.addAtRec(ListNode.java:55)
at ListNode.addAtRec(ListNode.java:55)
at List.addAtRec(List.java:158)

What did I do wrong? Thanks for your time and answers.


回答1:


You have two errors in your recursive method:

  1. Before calling addAtRec(obj, k, current); you should decrease k by 1, so it would be better to call k-- before this line.

  2. Once you have reached the base case (when k == 0)and executed the logic to add the new node, your recursive method must stop, probably with a simple return; statement. In this case, you're not stopping it so you will call it every time until get to the end of your list.

Based on these 2 advices, your code should look like:

public void addAtRec(Object obj, int k, ListNode current)
    throws ListIndexOutOfBoundsException {
    //always use braces even for single line block of code
    if(current.nextNode == null && k != 0) {
        throw new ListIndexOutOfBoundsException();
    }
    if(k == 0) {
        ListNode l = new ListNode(obj);
        l.nextNode = current.nextNode;
        current.nextNode = l;
        //stopping the recursion
        return;
    }
    current = current.nextNode;
    //decrease k by 1 before calling your method recursively
    k--;
    addAtRec(obj, k, current);
}

This is not part of the main problem, but IMO your methods to add nodes in the list should belong to the List class and not to theListNode. Remember that the data structure that holds the nodes and decide how to tie them will be the List, not the nodes by themselves.



来源:https://stackoverflow.com/questions/17261687/recursively-add-a-node-at-a-certain-index-on-linked-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!