coda con priorita' creata. bisogna modificare la unsorted poiche' e' stata copiata dalla prima.

This commit is contained in:
2014-04-02 00:52:49 +02:00
parent 5e892f4961
commit c193420828
4 changed files with 179 additions and 3 deletions

View File

@@ -61,9 +61,10 @@ public class SortedListPriorityQueue<K,V> implements PriorityQueue<K,V> {
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<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;
}
}

View 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;
}
}