prima parte seconda prova intercorso
This commit is contained in:
@@ -18,7 +18,7 @@ import java.util.Iterator;
|
||||
*/
|
||||
|
||||
//Complete implementation by xgiovio
|
||||
//Collisions resolved via chaining with LogFile
|
||||
//Collisions resolved via chaining with LogFile.java
|
||||
|
||||
|
||||
public class ChainingHashTable<K,V> implements Dictionary<K,V> {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package iterator;
|
||||
|
||||
import exceptions.BoundaryViolationException;
|
||||
import javafx.geometry.Pos;
|
||||
import position.Position;
|
||||
import position.PositionList;
|
||||
|
||||
|
||||
167
test_2_prova_intercorso/ExPQ_21_2_11.java
Executable file
167
test_2_prova_intercorso/ExPQ_21_2_11.java
Executable file
@@ -0,0 +1,167 @@
|
||||
package test_2_prova_intercorso;
|
||||
import exceptions.*;
|
||||
|
||||
/*Il programma deve stampare (l'ordine in cui vengono stampate le entrate su ciascuna linea e` ininfluente):
|
||||
|
||||
I test
|
||||
Errore: almeno una delle code a priorita` e` vuota
|
||||
|
||||
II test
|
||||
La coda a priorita` Q1 contiene le entrate:
|
||||
(1,1) (3,3) (5,5) (7,7) (9,9) (11,11) (13,13)
|
||||
La coda a priorita` Q2 contiene le entrate:
|
||||
(1,1) (5,5) (9,9) (13,13) (17,17) (21,21) (25,25) (29,29)
|
||||
Dopo aver invocato subtract(Q1,Q2), la coda a priorita` Q1 contiene le entrate:
|
||||
(3,3) (7,7) (11,11)
|
||||
|
||||
III test
|
||||
La coda a priorita` Q1 contiene le entrate:
|
||||
(3,1) (5,1) (7,1) (7,2) (9,1) (9,2) (9,3) (11,1) (11,2) (11,3) (13,1) (13,2) (13,3) (13,4) (15,1) (15,2) (15,3) (15,4) (15,5) (25,54)
|
||||
La coda a priorita` Q2 contiene le entrate:
|
||||
(5,1) (9,1) (9,2) (13,1) (13,2) (13,3) (17,1) (17,2) (17,3) (17,4) (21,1) (21,2) (21,3) (21,4) (21,5) (25,1) (25,2) (25,3) (25,4) (25,5) (25,6)
|
||||
Dopo aver invocato subtract(Q1,Q2), la coda a priorita` Q1 contiene le entrate:
|
||||
(3,1) (7,2) (7,1) (11,1) (11,2) (11,3) (15,3) (15,5) (15,1) (15,2) (15,4)
|
||||
|
||||
|
||||
*/
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import java.util.Comparator;
|
||||
|
||||
|
||||
import priorityqueue.Entry;
|
||||
import priorityqueue.PriorityQueue;
|
||||
import priorityqueue.heap.HeapPriorityQueue;
|
||||
|
||||
public class ExPQ_21_2_11 {
|
||||
public static void main(String[] args) throws InvalidKeyException{
|
||||
PriorityQueue <Integer,Integer> Q1 = new HeapPriorityQueue<Integer,Integer>(new IntegerComparator());
|
||||
PriorityQueue <Integer,Integer> Q2 = new HeapPriorityQueue<Integer,Integer>(new IntegerComparator());
|
||||
Q2.insert(1, 5) ;
|
||||
|
||||
|
||||
System.out.println("I test");
|
||||
try{
|
||||
subtract(Q1,Q2, new IntegerComparator());
|
||||
}
|
||||
catch (EmptyPriorityQueueException err){
|
||||
System.out.println("Errore: almeno una delle code a priorita` e` vuota");
|
||||
}
|
||||
|
||||
System.out.println("\nII test");
|
||||
System.out.println("La coda a priorita` Q1 contiene le entrate:");
|
||||
for(int k=1;k<=14;k=k+2)
|
||||
{
|
||||
Q1.insert(k,k);
|
||||
System.out.print("("+k+","+k+") ");
|
||||
}
|
||||
|
||||
System.out.println("\nLa coda a priorita` Q2 contiene le entrate:");
|
||||
for(int k=1;k<=30;k=k+4)
|
||||
{
|
||||
Q2.insert(k,k);
|
||||
System.out.print("("+k+","+k+") ");
|
||||
}
|
||||
System.out.println("\nDopo aver invocato subtract(Q1,Q2), la coda a priorita` Q1 contiene le entrate:");
|
||||
subtract(Q1,Q2,new IntegerComparator());
|
||||
while(!Q1.isEmpty())
|
||||
System.out.print("("+Q1.min().getKey()+","+Q1.removeMin().getValue()+") ");
|
||||
|
||||
|
||||
Q1= new HeapPriorityQueue<Integer,Integer>(new IntegerComparator());
|
||||
Q2 = new HeapPriorityQueue<Integer,Integer>(new IntegerComparator());
|
||||
System.out.println("\n\nIII test");
|
||||
System.out.println("La coda a priorita` Q1 contiene le entrate:");
|
||||
for(int k=1;k<=15;k=k+2)
|
||||
for(int v=1;v<=k/3;v++){
|
||||
Q1.insert(k,v);
|
||||
System.out.print("("+k+","+v+") ");
|
||||
}
|
||||
Q1.insert(25,54);
|
||||
System.out.print("(25,54) ");
|
||||
System.out.println("\nLa coda a priorita` Q2 contiene le entrate:");
|
||||
for(int k=1;k<=25;k=k+4)
|
||||
for(int v=1;v<=k/4;v++){
|
||||
Q2.insert(k,v);
|
||||
System.out.print("("+k+","+v+") ");
|
||||
}
|
||||
|
||||
System.out.println("\nDopo aver invocato subtract(Q1,Q2), la coda a priorita` Q1 contiene le entrate:");
|
||||
subtract(Q1,Q2,new IntegerComparator());
|
||||
while(!Q1.isEmpty())
|
||||
System.out.print("("+Q1.min().getKey()+","+Q1.removeMin().getValue()+") ");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static class IntegerComparator implements Comparator<Integer> {
|
||||
|
||||
public int compare(Integer a, Integer b) throws ClassCastException {
|
||||
return(a-b);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static <K,V>void subtract (PriorityQueue<K,V> Q1, PriorityQueue <K,V> Q2,Comparator<K>c ) throws InvalidKeyException{
|
||||
|
||||
if (Q1== null || Q2 == null || Q1.size() == 0 || Q2.size() == 0)
|
||||
throw new EmptyPriorityQueueException();
|
||||
|
||||
PriorityQueue<K,V> tempq = new HeapPriorityQueue<K, V>();
|
||||
|
||||
Entry<K,V> t = Q1.removeMin();
|
||||
Entry<K,V> t2 = Q2.removeMin();
|
||||
|
||||
|
||||
for (; ;){
|
||||
|
||||
int result = c.compare( t.getKey(),t2.getKey());
|
||||
|
||||
if (result < 0) {
|
||||
tempq.insert(t.getKey(), t.getValue());
|
||||
if (Q1.size() > 0) {
|
||||
t = Q1.removeMin();
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (result == 0){
|
||||
if (Q1.size() > 0) {
|
||||
t = Q1.removeMin();
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( result > 0){
|
||||
if (Q2.size() > 0) {
|
||||
|
||||
t2 = Q2.removeMin();
|
||||
continue;
|
||||
}else {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
for ( ;tempq.size() > 0 ;){
|
||||
t = tempq.removeMin();
|
||||
Q1.insert(t.getKey(), t.getValue());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user