Rimosso ArraySequenceFake. Implementato positions() in NodePositionList. Creato il DefaultComparator. Prima implementazione di SortedPriorityList
This commit is contained in:
37
priorityqueue/MyEntry.java
Normal file
37
priorityqueue/MyEntry.java
Normal 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;
|
||||
|
||||
}
|
||||
88
priorityqueue/SortedListPriorityQueue.java
Normal file
88
priorityqueue/SortedListPriorityQueue.java
Normal 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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user