2017-06-29 203 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可以很容易地与它混淆。