coda con priorita' creata. bisogna modificare la unsorted poiche' e' stata copiata dalla prima.
This commit is contained in:
@@ -3,7 +3,6 @@ package com.xgiovio;
|
|||||||
import general_utility.test_object;
|
import general_utility.test_object;
|
||||||
import position.Position;
|
import position.Position;
|
||||||
import sequence.ArraySequence;
|
import sequence.ArraySequence;
|
||||||
import sequence.ArraySequenceFake;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created with xgiovio.macbookair.
|
* Created with xgiovio.macbookair.
|
||||||
|
|||||||
41
com/xgiovio/SortedListPriorityQueueTest.java
Normal file
41
com/xgiovio/SortedListPriorityQueueTest.java
Normal file
@@ -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<Integer,String> a = new SortedListPriorityQueue<Integer, String>();
|
||||||
|
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());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -61,9 +61,10 @@ public class SortedListPriorityQueue<K,V> implements PriorityQueue<K,V> {
|
|||||||
for (; itp.hasNext(); ) {
|
for (; itp.hasNext(); ) {
|
||||||
temp_pos = itp.next();
|
temp_pos = itp.next();
|
||||||
status = c.compare(temp_pos.element().getKey(), key);
|
status = c.compare(temp_pos.element().getKey(), key);
|
||||||
if (status > 0)
|
if (status > 0) {
|
||||||
data.addBefore(temp_pos, t);
|
data.addBefore(temp_pos, t);
|
||||||
return t;
|
return t;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
data.addLast(t);
|
data.addLast(t);
|
||||||
return t;
|
return t;
|
||||||
@@ -85,4 +86,27 @@ public class SortedListPriorityQueue<K,V> implements PriorityQueue<K,V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
Iterator<MyEntry<K, V>> it = data.iterator();
|
||||||
|
String to_return = "";
|
||||||
|
to_return = to_return + "[ ";
|
||||||
|
|
||||||
|
for (;it.hasNext();){
|
||||||
|
MyEntry<K, V> t = it.next();
|
||||||
|
|
||||||
|
if (it.hasNext()) {
|
||||||
|
|
||||||
|
to_return+=(t.toString() + " , ");
|
||||||
|
} else {
|
||||||
|
|
||||||
|
to_return+=(t.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
to_return = to_return + " ]";
|
||||||
|
|
||||||
|
return to_return;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
112
priorityqueue/UnsortedListPriorityQueue.java
Normal file
112
priorityqueue/UnsortedListPriorityQueue.java
Normal file
@@ -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<K,V> implements PriorityQueue<K,V> {
|
||||||
|
|
||||||
|
private NodePositionList<MyEntry<K,V>> data = new NodePositionList<MyEntry<K, V>>();
|
||||||
|
private Comparator<K> c;
|
||||||
|
|
||||||
|
public UnsortedListPriorityQueue(){
|
||||||
|
c = new DefaultComparator<K>();
|
||||||
|
}
|
||||||
|
public UnsortedListPriorityQueue(Comparator<K> comp){
|
||||||
|
c = comp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return data.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return data.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Entry<K, V> min() throws EmptyPriorityQueueException {
|
||||||
|
if (isEmpty()){
|
||||||
|
throw new EmptyPriorityQueueException();
|
||||||
|
} else {
|
||||||
|
return data.first().element();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Entry<K, V> insert(K key, V value) throws InvalidKeyException {
|
||||||
|
try {
|
||||||
|
MyEntry<K, V> t = new MyEntry<K, V>(key, value);
|
||||||
|
if (data.size() == 0) {
|
||||||
|
data.addFirst(t);
|
||||||
|
return t;
|
||||||
|
} else {
|
||||||
|
Iterator<Position<MyEntry<K, V>>> itp = data.positions();
|
||||||
|
int status;
|
||||||
|
Position<MyEntry<K, V>> 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<K, V> removeMin() throws EmptyPriorityQueueException {
|
||||||
|
|
||||||
|
if (isEmpty()){
|
||||||
|
throw new EmptyPriorityQueueException();
|
||||||
|
} else {
|
||||||
|
return data.remove(data.first());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
Iterator<MyEntry<K, V>> it = data.iterator();
|
||||||
|
String to_return = "";
|
||||||
|
to_return = to_return + "[ ";
|
||||||
|
|
||||||
|
for (;it.hasNext();){
|
||||||
|
MyEntry<K, V> t = it.next();
|
||||||
|
|
||||||
|
if (it.hasNext()) {
|
||||||
|
|
||||||
|
to_return+=(t.toString() + " , ");
|
||||||
|
} else {
|
||||||
|
|
||||||
|
to_return+=(t.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
to_return = to_return + " ]";
|
||||||
|
|
||||||
|
return to_return;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user