Rimosso ArraySequenceFake. Implementato positions() in NodePositionList. Creato il DefaultComparator. Prima implementazione di SortedPriorityList

This commit is contained in:
2014-04-01 22:06:51 +02:00
parent 9836790893
commit 5e892f4961
10 changed files with 230 additions and 305 deletions

View File

@@ -0,0 +1,37 @@
package priorityqueue;
/**
* Created with MONSTER.
* User: xgiovio
* Date: 01/04/2014
* Time: 21:00
*/
public class MyEntry<K,V> implements Entry<K,V> {
public MyEntry(K a, V b){
key = a;
value = b;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public String toString() {
return ("" + key + " - " + value);
}
private K key;
private V value;
}

View File

@@ -0,0 +1,88 @@
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 SortedListPriorityQueue<K,V> implements PriorityQueue<K,V> {
private NodePositionList<MyEntry<K,V>> data = new NodePositionList<MyEntry<K, V>>();
private Comparator<K> c;
public SortedListPriorityQueue(){
c = new DefaultComparator<K>();
}
public SortedListPriorityQueue(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());
}
}
}