From c19342082851da94f58c79630198b20f627227aa Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Wed, 2 Apr 2014 00:52:49 +0200 Subject: [PATCH] coda con priorita' creata. bisogna modificare la unsorted poiche' e' stata copiata dalla prima. --- com/xgiovio/ArraySequenceTest.java | 1 - com/xgiovio/SortedListPriorityQueueTest.java | 41 +++++++ priorityqueue/SortedListPriorityQueue.java | 28 ++++- priorityqueue/UnsortedListPriorityQueue.java | 112 +++++++++++++++++++ 4 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 com/xgiovio/SortedListPriorityQueueTest.java create mode 100644 priorityqueue/UnsortedListPriorityQueue.java diff --git a/com/xgiovio/ArraySequenceTest.java b/com/xgiovio/ArraySequenceTest.java index 218bde9..0499158 100644 --- a/com/xgiovio/ArraySequenceTest.java +++ b/com/xgiovio/ArraySequenceTest.java @@ -3,7 +3,6 @@ package com.xgiovio; import general_utility.test_object; import position.Position; import sequence.ArraySequence; -import sequence.ArraySequenceFake; /** * Created with xgiovio.macbookair. diff --git a/com/xgiovio/SortedListPriorityQueueTest.java b/com/xgiovio/SortedListPriorityQueueTest.java new file mode 100644 index 0000000..ef1feb9 --- /dev/null +++ b/com/xgiovio/SortedListPriorityQueueTest.java @@ -0,0 +1,41 @@ +package com.xgiovio; + +import priorityqueue.SortedListPriorityQueue; + +import java.security.InvalidKeyException; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 02/04/2014 + * Time: 00:10 + */ +public class SortedListPriorityQueueTest { + + public static void main(String[] args) throws InvalidKeyException{ + + + SortedListPriorityQueue a = new SortedListPriorityQueue(); + a.insert(5,"hello"); + System.out.print(a.size()); + System.out.print(a); + a.insert(11,"sdas"); + System.out.print(a.size()); + + a.insert(21,"sdas"); + a.insert(1,"sdas"); + a.insert(-10,"slkjldas"); + + System.out.print(a); + System.out.print(a.size()); + a.insert(21,"sdas"); + a.insert(1,"sdas"); + a.insert(-10,"slkjldas"); + System.out.print(a); + System.out.print(a.size()); + + + + } + +} diff --git a/priorityqueue/SortedListPriorityQueue.java b/priorityqueue/SortedListPriorityQueue.java index addbda7..693363b 100644 --- a/priorityqueue/SortedListPriorityQueue.java +++ b/priorityqueue/SortedListPriorityQueue.java @@ -61,9 +61,10 @@ public class SortedListPriorityQueue implements PriorityQueue { for (; itp.hasNext(); ) { temp_pos = itp.next(); status = c.compare(temp_pos.element().getKey(), key); - if (status > 0) + if (status > 0) { data.addBefore(temp_pos, t); - return t; + return t; + } } data.addLast(t); return t; @@ -85,4 +86,27 @@ public class SortedListPriorityQueue implements PriorityQueue { } } + + @Override + public String toString() { + Iterator> it = data.iterator(); + String to_return = ""; + to_return = to_return + "[ "; + + for (;it.hasNext();){ + MyEntry t = it.next(); + + if (it.hasNext()) { + + to_return+=(t.toString() + " , "); + } else { + + to_return+=(t.toString()); + } + } + to_return = to_return + " ]"; + + return to_return; + + } } diff --git a/priorityqueue/UnsortedListPriorityQueue.java b/priorityqueue/UnsortedListPriorityQueue.java new file mode 100644 index 0000000..bfe4d28 --- /dev/null +++ b/priorityqueue/UnsortedListPriorityQueue.java @@ -0,0 +1,112 @@ +package priorityqueue; + +import exceptions.EmptyPriorityQueueException; +import position.NodePositionList; +import position.Position; +import utility.DefaultComparator; + +import java.security.InvalidKeyException; +import java.util.Comparator; +import java.util.Iterator; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 01/04/2014 + * Time: 20:57 + */ +public class UnsortedListPriorityQueue implements PriorityQueue { + + private NodePositionList> data = new NodePositionList>(); + private Comparator c; + + public UnsortedListPriorityQueue(){ + c = new DefaultComparator(); + } + public UnsortedListPriorityQueue(Comparator comp){ + c = comp; + } + + + @Override + public int size() { + return data.size(); + } + + @Override + public boolean isEmpty() { + return data.isEmpty(); + } + + @Override + public Entry min() throws EmptyPriorityQueueException { + if (isEmpty()){ + throw new EmptyPriorityQueueException(); + } else { + return data.first().element(); + } + } + + @Override + public Entry insert(K key, V value) throws InvalidKeyException { + try { + MyEntry t = new MyEntry(key, value); + if (data.size() == 0) { + data.addFirst(t); + return t; + } else { + Iterator>> itp = data.positions(); + int status; + Position> temp_pos = null; + for (; itp.hasNext(); ) { + temp_pos = itp.next(); + status = c.compare(temp_pos.element().getKey(), key); + if (status > 0) { + data.addBefore(temp_pos, t); + return t; + } + } + data.addLast(t); + return t; + + } + } + catch (ClassCastException err){ + throw new InvalidKeyException(); + } + } + + @Override + public Entry removeMin() throws EmptyPriorityQueueException { + + if (isEmpty()){ + throw new EmptyPriorityQueueException(); + } else { + return data.remove(data.first()); + } + } + + + @Override + public String toString() { + Iterator> it = data.iterator(); + String to_return = ""; + to_return = to_return + "[ "; + + for (;it.hasNext();){ + MyEntry t = it.next(); + + if (it.hasNext()) { + + to_return+=(t.toString() + " , "); + } else { + + to_return+=(t.toString()); + } + } + to_return = to_return + " ]"; + + return to_return; + + } +}