Creata unsorted position list
This commit is contained in:
@@ -43,7 +43,26 @@ public class UnsortedListPriorityQueue<K,V> implements PriorityQueue<K,V> {
|
||||
if (isEmpty()){
|
||||
throw new EmptyPriorityQueueException();
|
||||
} else {
|
||||
return data.first().element();
|
||||
Iterator<Position<MyEntry<K, V>>> itp = data.positions();
|
||||
int status;
|
||||
Position<MyEntry<K, V>> min = null;
|
||||
Position<MyEntry<K, V>> 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<K,V> implements PriorityQueue<K,V> {
|
||||
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;
|
||||
|
||||
}
|
||||
data.addLast(t);
|
||||
return t;
|
||||
}
|
||||
catch (ClassCastException err){
|
||||
throw new InvalidKeyException();
|
||||
@@ -78,12 +80,29 @@ public class UnsortedListPriorityQueue<K,V> implements PriorityQueue<K,V> {
|
||||
|
||||
@Override
|
||||
public Entry<K, V> removeMin() throws EmptyPriorityQueueException {
|
||||
|
||||
if (isEmpty()){
|
||||
throw new EmptyPriorityQueueException();
|
||||
} else {
|
||||
return data.remove(data.first());
|
||||
Iterator<Position<MyEntry<K, V>>> itp = data.positions();
|
||||
int status;
|
||||
Position<MyEntry<K, V>> min = null;
|
||||
Position<MyEntry<K, V>> 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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user