From 7f3a0bfc24dbbfa1c8596b60e15c9695963826d5 Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Sun, 6 Apr 2014 20:38:04 +0200 Subject: [PATCH] Creata unsorted position list --- com/xgiovio/NodePositionListTest.java | 48 +++++--------- .../UnsortedListPriorityQueueTest.java | 52 +++++++++++++++ com/xgiovio/global.java | 36 +++++++++++ priorityqueue/UnsortedListPriorityQueue.java | 63 ++++++++++++------- 4 files changed, 145 insertions(+), 54 deletions(-) create mode 100644 com/xgiovio/UnsortedListPriorityQueueTest.java diff --git a/com/xgiovio/NodePositionListTest.java b/com/xgiovio/NodePositionListTest.java index 6296648..57ee315 100644 --- a/com/xgiovio/NodePositionListTest.java +++ b/com/xgiovio/NodePositionListTest.java @@ -16,42 +16,26 @@ import java.util.Iterator; public class NodePositionListTest { public static void main(String[] args) { - NodePositionList a = new NodePositionList(); + NodePositionList a = new NodePositionList(); - a.addLast(new test_object(1)); - a.addLast(new test_object(2)); - a.addLast(new test_object(3)); - a.addLast(new test_object(4)); + a.addLast(new Integer(1)); + a.addLast(new Integer(1)); + a.addLast(new Integer(1)); + a.addLast(new Integer(2)); + a.addLast(new Integer(2)); + a.addLast(new Integer(2)); + a.addLast(new Integer(3)); + a.addLast(new Integer(4)); + a.addLast(new Integer(4)); + a.addLast(new Integer(4)); + a.addLast(new Integer(4)); + a.addLast(new Integer(4)); + + + global.cancellaDuplicati(a); System.out.print(a); - a.reverse(); - System.out.print(a); - - Iterator it = a.iterator(); - System.out.print(it.hasNext()); - System.out.print(it.next()); - System.out.print(it.hasNext()); - System.out.print(it.next()); - System.out.print(it.hasNext()); - System.out.print(it.next()); - System.out.print(it.hasNext()); - System.out.print(it.next()); - System.out.print(it.hasNext()); - - Iterator> itp = a.positions(); - System.out.print(itp.hasNext()); - System.out.print(itp.next().element()); - System.out.print(itp.hasNext()); - System.out.print(itp.next().element()); - System.out.print(itp.hasNext()); - System.out.print(itp.next().element()); - System.out.print(itp.hasNext()); - System.out.print(itp.next().element()); - System.out.print(itp.hasNext()); - - - diff --git a/com/xgiovio/UnsortedListPriorityQueueTest.java b/com/xgiovio/UnsortedListPriorityQueueTest.java new file mode 100644 index 0000000..4e57957 --- /dev/null +++ b/com/xgiovio/UnsortedListPriorityQueueTest.java @@ -0,0 +1,52 @@ +package com.xgiovio; + +import priorityqueue.SortedListPriorityQueue; +import priorityqueue.UnsortedListPriorityQueue; + +import java.security.InvalidKeyException; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 02/04/2014 + * Time: 00:10 + */ +public class UnsortedListPriorityQueueTest { + + public static void main(String[] args) throws InvalidKeyException{ + + + UnsortedListPriorityQueue a = new UnsortedListPriorityQueue(); + a.insert(5,"hello"); + System.out.println(a.size()); + System.out.println(a); + a.insert(11,"sdas"); + System.out.println(a.size()); + + a.insert(21,"sdas"); + a.insert(1,"sdas"); + a.insert(-20,"slkjldas"); + + System.out.println(a); + System.out.println(a.size()); + a.insert(21,"sdas"); + a.insert(-34,"sdas"); + a.insert(45,"slkjldas"); + System.out.println(a); + System.out.println(a.size()); + + System.out.println(a.min()); + + System.out.println(a.size()); + a.removeMin(); + System.out.println(a.size()); + System.out.println(a); + System.out.println(a.min()); + a.removeMin(); + System.out.println(a); + System.out.println(a.size()); + System.out.println(a.min()); + + } + +} diff --git a/com/xgiovio/global.java b/com/xgiovio/global.java index b1f0e0c..e987cdc 100644 --- a/com/xgiovio/global.java +++ b/com/xgiovio/global.java @@ -1,7 +1,12 @@ package com.xgiovio; +import position.Position; +import position.PositionList; import sequence.Sequence; +import java.util.Iterator; +import java.util.NoSuchElementException; + /** * Created with MONSTER. * User: xgiovio @@ -19,4 +24,35 @@ class global { } + + + public static void cancellaDuplicati (PositionListL){ + + if (L.size() >=2){ + Position first; + Position second; + first = L.first(); + second = L.next(first); + for (;true;) { + if (first.element().equals(second.element())) { + L.remove(second); + if (first!= L.last()) { + second = L.next(first); + }else { + break; + } + } else { + first = second; + if (first!= L.last()) { + second = L.next(second); + }else { + break; + } + + } + } + } + + } + } diff --git a/priorityqueue/UnsortedListPriorityQueue.java b/priorityqueue/UnsortedListPriorityQueue.java index bfe4d28..1ff38f1 100644 --- a/priorityqueue/UnsortedListPriorityQueue.java +++ b/priorityqueue/UnsortedListPriorityQueue.java @@ -43,7 +43,26 @@ public class UnsortedListPriorityQueue implements PriorityQueue { if (isEmpty()){ throw new EmptyPriorityQueueException(); } else { - return data.first().element(); + Iterator>> itp = data.positions(); + int status; + Position> min = null; + Position> temp_pos = null; + int flag = 0; + for (; itp.hasNext(); ) { + if (flag == 0){ + flag = 1; + min = itp.next(); + } else { + temp_pos = itp.next(); + status = c.compare(temp_pos.element().getKey(), min.element().getKey()); + if (status < 0) { + min = temp_pos; + } + } + } + return min.element(); + + } } @@ -51,25 +70,8 @@ public class UnsortedListPriorityQueue implements PriorityQueue { 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; - - } + data.addLast(t); + return t; } catch (ClassCastException err){ throw new InvalidKeyException(); @@ -78,12 +80,29 @@ public class UnsortedListPriorityQueue implements PriorityQueue { @Override public Entry removeMin() throws EmptyPriorityQueueException { - if (isEmpty()){ throw new EmptyPriorityQueueException(); } else { - return data.remove(data.first()); + Iterator>> itp = data.positions(); + int status; + Position> min = null; + Position> temp_pos = null; + int flag = 0; + for (; itp.hasNext(); ) { + if (flag == 0){ + flag = 1; + min = itp.next(); + } else { + temp_pos = itp.next(); + status = c.compare(temp_pos.element().getKey(), min.element().getKey()); + if (status < 0) { + min = temp_pos; + } + } + } + return data. remove( min ); } + }