2013-02-24 26 views
0

我在使用此代码时遇到问题。我必须创建一个PriorityQ,它具有快速的O(1)插入时间,但高优先级项目的移除速度较慢,任何人都可以告诉我我做错了什么?PriorityQ Assistance

public class PriorityQ { 
private long[] pq; 
private int nElems; 

public void PQ() { 
    pq= new long[3]; 
    nElems=0; 
    } 

public boolean isEmpty() { return nElems==0;} 

public void insert(long x) { 
    pq[nElems++]=x; 
} 
public long remove(long x){ 
long max = 0; 
     for (int i = 1; i < nElems; i++) 
      if (pq[i] == x) { 
       max = pq[i]; 
       pq[i] = pq[--nElems]; 
       break; 
      } 

     return max; 
} 
public static void main (String []args){ 
    PriorityQ theQ = new PriorityQ(); 
    theQ.insert (10); 
    theQ.insert (20); 
    theQ.insert (30); 
    theQ.remove (10); 

     for(int i=0; i<theQ.nElems; i++){ 
     System.out.print (""); 
     System.out.print (theQ.pq[i]); 
     } 

}}

回答

0

对于初学者来说,你是不是正确地定义你的构造。

public void PQ() { 
pq= new long[3]; 
nElems=0; 
} 

应该更像

PriorityQ() { 
pq= new long[3]; 
nElems=0; 
}