Salve, io devo fare questo esercizio:
"Scrivere la funzione public static <K,V> PositionList<Entry<K,V> > sort(SortedListPriorityQueue<K,V>
Q1, SortedListPriorityQueue<K,V> Q2, Comparator <K> c).
Indicazioni per lo svolgimento dell’esercizio:
• La funzione deve prendere in input due code a priorità Q1 e Q2 e un comparatore c
(dello stesso tipo usato dalle due code) e restituire una lista contenente tutte le entrate di
Q1 e Q2 ordinate in base ai valori delle chiavi.
• La funzione deve avere complessità O(n) dove n è il numero totale di elementi di Q1 e
Q2."
La mia implementazione è questa:
codice:
public class Sort {
public static void main(String[] args) {
DefaultComparator<Integer> c = new DefaultComparator<Integer>();
PriorityQueue<Integer, Integer> p1 = new SortedListPriorityQueue<Integer, Integer>(c);
PriorityQueue<Integer, Integer> p2 = new SortedListPriorityQueue<Integer, Integer>(c);
p1.insert(1, 1);
p1.insert(1, 2);
p1.insert(3, 2);
p1.insert(3, 1);
p2.insert(5, 3);
p2.insert(7, 4);
p2.insert(7, 3);
p2.insert(9, 5);
NodePositionList<Entry<Integer, Integer>> list = sort(p1,p2,c);
System.out.println(p2.toString());
}
public static <K,V> PositionList<Entry<K,V>> sort(SortedListPriorityQueue<K,V> Q1, SortedListPriorityQueue<K,V> Q2, Comparator<K> c) {
PositionList<Entry<K,V>> list = new NodePositionList<>();
while(Q1.isEmpty() && Q2.isEmpty()) {
if(c.compare(Q1.min().getKey(), Q2.min().getKey()) < 0)
list.addLast(Q1.removeMin());
else list.addLast(Q1.removeMin());
}
if(!Q1.isEmpty()) {
int size = Q1.size();
for(int i = 0; i < size; i++) {
list.addLast(Q1.removeMin());
}
} else {
int size = Q2.size();
for(int i = 0; i < size; i++) {
list.addLast(Q2.removeMin());
}
}
return list;
}
}
Il problema è che mi dà errore alla funzione sort, dicendo che devo cambiare i parametri del metodo sort a SortedListPriorityQueue<Integer, Integer> Q1, SortedListPriorityQueue<Integer, Integer> Q2, Comparator<Integer> c. Sono sicuro sia una sciocchezza, ma non riesco a trovare una soluzione. Grazie a tutti .