簡體   English   中英

遞歸和反向遞歸循環通過hasNext()方法

[英]Recursion and Reverse Recursion looping through hasNext() method

大家好我是java的新手,所以我真的很感激任何幫助。 好的,這就是我遇到的問題:我有一個列表類和一個listNode類,列表Class由一個名字,一個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()),它顯示從頭到尾的對象(一個遞歸方法),我不認為有可能做一個有前一個方法所以我有點堅持這個方法。 我真的很感謝任何Ideas Thanks家伙

如果允許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很容易與它混淆。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM