2014-12-02 102 views
0

所以我最近了解了泛型,並認爲在優先級隊列中實現它們會很酷。我有一個「Block」元素,它有一個firstName和一個數據變量。優先級隊列的節點由Block,Next和Prev組成。Priority Queue通用

我附上下面的代碼。我幾乎完全得到「應該參數化」的錯誤/警告。還有一個錯誤,說我的「數據」元素無法解析到一個字段,這可能意味着我無法告訴我要阻止我在節點中的「元素E」。任何建議將深表讚賞

package QGen; 

public class Block<E> implements Comparable<Block<E>> { 
    protected String firstName; 
    protected int data; 

    public Block(String firstName, int data) { 
     super(); 
     this.firstName = firstName; 
     this.data = data; 

    } 

    @Override 
    public int compareTo(Block x) { 
     return (this.data - x.data); 
    } 
} 


package QGen; 

public class PriorityQueue<E extends Comparable> { 
    protected Node<E> firstSentinel; 
    protected Node<E> lastSentinel; 

    protected class Node<E> { 
     protected Node<E> next; 
     protected Node<E> prev; 
     private E element; 

     public Node(E e, Node<E> previous, Node<E> nextt) { 
      element = e; 
      prev = previous; 
      next = nextt; 
     } 
    } 

    public PriorityQueue() { 
     firstSentinel = new Node<>(null, null, null); 
     lastSentinel = new Node<>(null, null, null); 
     firstSentinel.data = 11111; 
     lastSentinel.data = 0; 
     firstSentinel.prev = null; 
     firstSentinel.next = lastSentinel; 
     lastSentinel.prev = firstSentinel; 
     lastSentinel.next = null; 
    } 

    public void enQueue(E x) { 
     Node<E> newX = new Node<E>(x, null, null); 
     if (firstSentinel.next == lastSentinel)// list is empyty 
     { 
      firstSentinel.next = newX; 
      newX.prev = firstSentinel; 
      newX.next = lastSentinel; 
      lastSentinel.prev = newX; 
     } else { 
      Node<E> temp = newX; 
      Node<E> curr = firstSentinel.next; 
      while (curr != lastSentinel && temp.element.compareTo(curr) <= 0) {// <=comparison 
       // replaced 
       curr = curr.next; 
      } 
      Node<E> tempCurr = curr; 
      temp.next = tempCurr; 
      temp.prev = tempCurr.prev; 
      tempCurr.prev.next = temp; 
      tempCurr.prev = temp; 

     } 
    } 

    public E deQueue() { 
     if (firstSentinel.next == lastSentinel) { 
      return null; 
     } else { 
      Node<E> temp = new Node<E>(null, null, null); 
      temp = firstSentinel.next; 
      firstSentinel.next = temp.next; 
      temp.next.prev = firstSentinel; 
      return temp.element; 
     } 
    } 

    public void printt() { 
     Node<E> temp = new Node<E>(null, null, null); 
     temp = firstSentinel.next; 
     while (temp != lastSentinel) { 
      System.out 
        .println(temp.element.firstName + " " + temp.element.data); 

      temp = temp.next; 
     } 
    } 
} 

package QGen; 

public class containsMain<E> { 

    public static void main(String[] args) { 

     PriorityQueue<Block> example = new PriorityQueue<Block>(); 
     Block dequedObject = new Block<>(null, null); 
     Block<Block> incomingName = new Block<>("r", 1); 
     example.enQueue(incomingName); 
     dequedObject = (Block) example.deQueue(); 

    } 

} 

我知道我的PriorityQueue可能不是最好的實現,我會改進它。這就是我不能拿出一個解決方案

感謝

回答

0

不看你的方法邏輯仿製藥,我有一個關於泛型幾點看法:

爲什麼是塊本身的通用?它不支持任何通用字段,因此將其從Block中移除!

new Node<>(null, null, null); 
// this is a bad idea, change it to new Node<E>(null, null, null) 


firstSentinel.data = 11111; 
lastSentinel.data = 0; 

//Those two cant work, because firstSentinel is referencing a Node<E> and not a Block! Delete those two rows, as they make no sense in your generic implementation of the Queue 

Block<Block> incomingName = new Block<>("r", 1); 
// This doesnt make sense, should be Block incomingName = new Block(...)