递归和反向递归循环通过hasNext()方法

时间:2017-06-29 14:40:49

标签: java list collections

大家好我是java的新手,所以我真的非常感谢任何帮助。  好的,这就是我遇到的问题: 我有一个列表类和一个listNode类,列表类由名称,firstNode和lastNode表示。 firstNode和lastNode来自listNode类型,listNode由Object(例如data或Object o)表示,nextNode指向列表中的下一个Node,它也来自listNode类型。

列表类:

public class List {

private ListNode firstNode;
private ListNode lastNode;
private String name;

public List() {
    this("list");
}

public List(String listName) {
    name = listName;
    firstNode = lastNode = null;
}

public void insertAtFront(Object insertItem) {
    if (isEmpty())
        firstNode = lastNode = new ListNode(insertItem);
    else
        firstNode = new ListNode(insertItem, firstNode);
}

public void insertAtBack(Object insertItem) {
    if (isEmpty())
        firstNode = lastNode = new ListNode(insertItem);
    else
        lastNode = lastNode.nextNode = new ListNode(insertItem);
}

public Object removeFromFront() throws EmptyListException {
    if (isEmpty())
        throw new EmptyListException(name);
    Object removedItem = firstNode.data;

    if (firstNode == lastNode)
        firstNode = lastNode = null;
    else
        firstNode = firstNode.nextNode;
    return removedItem;
}

public Object removeFromBack() throws EmptyListException {
    if (isEmpty())
        throw new EmptyListException(name);

    Object removedItem = lastNode.data;
    if (firstNode == lastNode)
        firstNode = lastNode = null;
    else {
        ListNode current = firstNode;

        while (current.nextNode != lastNode)
            current = current.nextNode;

        lastNode = current;
        current.nextNode = null;
    }
    return removedItem;
}

public boolean isEmpty() {
    return firstNode == null;
}

public void print() {
    if (isEmpty()) {
        System.out.printf("Empty %s\n", name);
        return;
    }
    System.out.printf("The %s is : ", name);
    ListNode current = firstNode;

    while (current != null) {
        System.out.printf("%s", current.data);
        current = current.nextNode;
    }
    System.out.println("\n");
}

@Override
public String toString() {
    String stk = "(";
    if(isEmpty())return "Empty List";
    ListNode checkNode = firstNode;
        while (checkNode != null) {
        stk += checkNode.data.toString()+ " , ";
        checkNode = checkNode.nextNode;
    }
    return stk+")";
}
public ListNode removeAt (int k){
    if(k<=0 || k>getLength())
        try{
            throw new IllegalValues();
        }catch(IllegalValues iv){
            iv.printStackTrace();
            return null;
        }
    ListNode newNode = firstNode;
    if (k==1) {
        ListNode removedNode = firstNode;
        firstNode = firstNode.nextNode;
        return removedNode;
    }
    ListNode someNode = firstNode;
    for (int i = 1; i < k - 1; i++) {
        someNode = someNode.nextNode;
    }
    ListNode removedNode = someNode.nextNode;
    someNode.nextNode = someNode.nextNode.nextNode;
    return removedNode;
}
public int getLength(){
    ListNode checkNode = firstNode;
    int count =0;
    while (checkNode != null) {
    count++;
    checkNode = checkNode.nextNode;
}
    return count;
}
public  void show(){
    if (firstNode==null)
      return;
    else
        System.out.print(firstNode + " ,");
      firstNode.show();
    }
public  void showRev(){
    if (lastNode==null)
      return;
    else
        System.out.println(lastNode + ",");
      lastNode.showRev();
    }
    }   

ListNode类

public class ListNode {

Object data;
ListNode nextNode;

public ListNode(Object o) {
    this(o, null);
}

public ListNode(Object o, ListNode node) {
    data = o;
    nextNode = node;
}

public Object getObject() {
    return data;
}

public ListNode getNext(){
    return nextNode;
}

public ListNode show() {
if(this.nextNode == null)return this;
ListNode displayMe = nextNode.show();
System.out.print(displayMe + " , ");
return displayMe;

}

public ListNode showRev() {
    if(this.firstNode == null)return this;
    ListNode displayMe = lastNode.show();
    System.out.print(displayMe + " , ");
    return displayMe;

}

}

我有一个名为show的递归方法,它从头到尾显示列表中的所有对象,现在我正在尝试制作类似的东西(方法名称是showRev()),它显示从最后到最后的对象开头(一个递归方法),我不认为有可能做一个有前一个方法所以我有点坚持这个方法。 我真的很感激任何想法 谢谢你们

1 个答案:

答案 0 :(得分:2)

如果允许您的showRev方法接受参数,那么我们可以将每个ListNode存储在java.util.List中:

public java.util.List<ListNode> showRev(java.util.List<ListNode> nodes) {
    if (this.nextNode == null) {
        Collections.reverse(nodes);

        System.out.println(nodes.stream().collect(Collectors.joining(" ")));

        return nodes;
    }

    nodes.add(lastNode.show());

    return showRev(nodes);
}

请注意,这里的递归没有什么特别之处,只是将ListNode添加到java.util.List

要调用此方法,只需将其传递给new ArrayList<>()

另外,我不会使用List作为课程的名称,因为java.util.List很容易与之混淆。