devo scrivere una funzione che prende in input due code a priorità Q1 e Q2 e un comparatore c dello stesso tipo usato per effettuare i confronti tra le chavi di Q1 e Q2. La funzione deve rimuovere da Q1 tutte le entrate le cui chiavi sono contenute in qualche entrata di Q2.
il codice che ho elaborato è questo:
mi dite che cosa non va? non finisce mai! sono disperata :-(
public static <K,V>void subtract (PriorityQueue<K,V> Q1, PriorityQueue <K,V> Q2 , Comparator<K>c){
PriorityQueue <K,V> P = new HeapPriorityQueue<K,V>();
Entry<K,V> m = null;
Entry<K,V> m1 = null;
while(!Q1.isEmpty()){
m=Q1.min();}
while(!Q2.isEmpty()){
m1=Q2.min();}
if(c.compare(m.getKey(), m1.getKey())<0){
P.insert(m.getKey(), m.getValue());
Q1.removeMin();
}
if(c.compare(m.getKey(), m1.getKey())==0){
Q1.removeMin();
}
if(c.compare(m.getKey(), m1.getKey())>0){
Q2.removeMin();
}
while(!P.isEmpty()){
Entry<K,V> e=P.removeMin();
K ke = e.getKey();
V ve = e.getValue();
Q1.insert(ke,ve);
}
}