From 015d8cac76373b55485bc4a5b099e653be2a6c8f Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Thu, 29 May 2014 19:40:53 +0200 Subject: [PATCH] prima parte seconda prova intercorso --- dictionary/ChainingHashTable.java | 2 +- iterator/IterablePosition.java | 1 - test_2_prova_intercorso/ExPQ_21_2_11.java | 167 ++++++++++++++++++++++ 3 files changed, 168 insertions(+), 2 deletions(-) create mode 100755 test_2_prova_intercorso/ExPQ_21_2_11.java diff --git a/dictionary/ChainingHashTable.java b/dictionary/ChainingHashTable.java index 53ad9f2..81c48f3 100644 --- a/dictionary/ChainingHashTable.java +++ b/dictionary/ChainingHashTable.java @@ -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 implements Dictionary { diff --git a/iterator/IterablePosition.java b/iterator/IterablePosition.java index a518673..5611261 100644 --- a/iterator/IterablePosition.java +++ b/iterator/IterablePosition.java @@ -1,7 +1,6 @@ package iterator; import exceptions.BoundaryViolationException; -import javafx.geometry.Pos; import position.Position; import position.PositionList; diff --git a/test_2_prova_intercorso/ExPQ_21_2_11.java b/test_2_prova_intercorso/ExPQ_21_2_11.java new file mode 100755 index 0000000..45ff162 --- /dev/null +++ b/test_2_prova_intercorso/ExPQ_21_2_11.java @@ -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 Q1 = new HeapPriorityQueue(new IntegerComparator()); + PriorityQueue Q2 = new HeapPriorityQueue(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(new IntegerComparator()); + Q2 = new HeapPriorityQueue(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 { + + public int compare(Integer a, Integer b) throws ClassCastException { + return(a-b); + + } + } + + public static void subtract (PriorityQueue Q1, PriorityQueue Q2,Comparatorc ) throws InvalidKeyException{ + + if (Q1== null || Q2 == null || Q1.size() == 0 || Q2.size() == 0) + throw new EmptyPriorityQueueException(); + + PriorityQueue tempq = new HeapPriorityQueue(); + + Entry t = Q1.removeMin(); + Entry 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()); + + } + + + + } + +}