2017-06-29 200 views
3

大家好我是新來的java所以我非常感謝任何幫助。 好吧,那麼這是我遇到的問題: 我有一個列表類和一個listNode類,列表Class由名稱,firstNode和lastNode表示。 firstNode和lastNode來自listNode類型,listNode由一個Object(例如數據或Object o)和一個nextNode指向列表中下一個也來自listNode類型的節點。通過hasNext()方法循環遞歸和反向遞歸

List類:

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; 

} 

} 

我有一個遞歸方法調用,顯示其顯示在列表中的所有對象從開始到結束,現在我想做類似的東西(方法名是showRev()),它顯示從結束到開始的對象(遞歸方法),我不認爲有可能做一個先前的方法,所以我有點卡住這種方法。 編號真的很感激任何想法 謝謝你們

+0

只是一個fyi:'show'不是[遞歸](http://pages.cs.wisc.edu/~calvin/cs110/RECURSION.html) – nem035

+0

類列表中的顯示不是遞歸的,但是它調用ListNode中的show方法遞歸 –

+0

Nope,它只是調用從同一個類創建但在不同實例上創建的方法。如果你調用'this.show',它會是遞歸的,調用'anotherNode.show'不是。 – nem035

回答

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可以很容易地與它混淆。