2017-04-17 65 views
0

我想調試一個程序的一部分窺探隊列對象從我實現自己的一個隊列類,所以我試圖迭代它並打印出所有元素,看看有什麼問題,而不改變隊列。我怎樣才能做到這一點?如何迭代我自己實現的隊列?

我Queue類(QueueLinkedList是名字):

public class QueueLinkedList<Customer> implements Queue<Customer> { 

    Node first, last; 

    public class Node { 
     Customer ele; 
     Node next; 
    } 

    public QueueLinkedList() {} 

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

    public QueueLinkedList<Customer> enqueue(Customer ele) { 
     Node current = last; 
     last = new Node(); 
     last.ele = ele; 
     last.next = null; 

     if (current == null) 
      first = last; 
     else 
      current.next = last; 

     return this; 
    } 

    public Customer dequeue() { 
     if (isEmpty()) 
      throw new java.util.NoSuchElementException(); 

     Customer ele = first.ele; 
     first = first.next;  
     return ele; 
    } 

    public Customer peek() { 
     Customer ele = first.ele; 
     return ele; 
    } 
+0

它錯過了一些代碼。 'QueueLinkedList()'構造函數應該在'QueueLinkedList'類中。在那裏,它不能很好地編譯。 – davidxxx

+0

這是QueueLinkedList類 –

+2

你必須實現'Iterable'接口併爲它創建一個'Iterator'。 –

回答

2

您使用鏈表來實現您的隊列。您可以迭代它,就像遍歷任何鏈表一樣。

public void iterate() { 
    Node iterator = first; 
    while(iterator != null) { 
     Customer customer = iterator.ele; 
     // do something with the customer 
     iterator = iterator.next; 
    } 
} 

編輯:如果你使用的情況下,需要返回迭代器那麼最好你應該實現Iterable接口。該解決方案已在另一個答案中提到。爲了將這個答案擴展到你的用例,我提供了下面的代碼。它會工作,但它不是「面向對象」的方式。

public class QueueLinkedList<Customer> implements Queue<Customer> { 

    private Node iterator; 

    // ... 

    public QueueLinkedList() { 
     iterator = null; 
     // ... 
    } 

    public Node iterator() { 
     iterator = first; 
     return iterator; 
    } 

    public boolean hasNext() { 
     return iterator != null; 
    } 

    public Node next() { 
     if(!hasNext()) { 
      throw new NoSuchElementException(); 
     } 
     Node next = iterator; 
     iterator = iterator.next(); 
     return next; 
    } 

} 

用法:

QueueLinkedList queue = new QueueLinkedList(); 
// ... 
Node iterator = queue.iterator(); 
while(queue.hasNext()) { 
    Node next = queue.next(); 
    Customer customer = next.ele; 
    // do something with the customer 
} 
+0

謝謝。但現在它說我需要使用返回Iterator的方法,所以像public Iterator iterate()這樣的東西。我應該添加什麼作爲回報聲明?我是否返回(Iterator)迭代器?或者我返回null或什麼? –

1

您需要實現Iterable<Customer>在您的隊列,如下圖所示,讓您的隊列可以重複相同的陣列和其他Java集合。

import java.util.*; 

public class QueueLinkedList<Customer> 
    implements Queue<Customer>, Iterable<Customer> 
{ 

    Node first, last; 

    public class Node { 
     Customer ele; 
     Node next; 
    } 

    class Iter implements Iterator<Customer> { 
     Node current = first; 

     public boolean hasNext() { 
      return current != null; 
     } 

     public Customer next() { 
      if (!hasNext()) 
       throw new NoSuchElementException(); 
      Customer next = current.ele; 
      current = current.next; 
      return next; 
     } 

     public void remove() { 
      throw new UnsupportedOperationException(); 
     } 
    } 

    public QueueLinkedList() {} 

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

    public QueueLinkedList<Customer> enqueue(Customer ele) { 
     Node current = last; 
     last = new Node(); 
     last.ele = ele; 
     last.next = null; 

     if (current == null) 
      first = last; 
     else 
      current.next = last; 

     return this; 
    } 

    public Customer dequeue() { 
     if (isEmpty()) 
      throw new java.util.NoSuchElementException(); 

     Customer ele = first.ele; 
     first = first.next;  
     return ele; 
    } 

    public Iterator<Customer> iterator { 
     return new Iter(); 
    } 
} 

注意,您必須聲明你的類的方式,Customer是一個通用類型參數,不是類Customer。這實際上是一件好事,因爲這意味着您可以使用您的QueueLinkedList類與任何數據類型。爲了清楚說明Customer是一個類型參數,您應該將每個出現的Customer替換爲包含單個大寫字母的類變量名稱,例如E

另外,如果你想QueueLinkedList永遠是Customer對象的隊列,你應該改變類聲明:

public class QueueLinkedList 
    implements Queue<Customer>, Iterable<Customer>