Creata unsorted position list
This commit is contained in:
@@ -16,42 +16,26 @@ import java.util.Iterator;
|
||||
public class NodePositionListTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
NodePositionList<test_object> a = new NodePositionList<test_object>();
|
||||
NodePositionList<Integer> a = new NodePositionList<Integer>();
|
||||
|
||||
a.addLast(new test_object(1));
|
||||
a.addLast(new test_object(2));
|
||||
a.addLast(new test_object(3));
|
||||
a.addLast(new test_object(4));
|
||||
a.addLast(new Integer(1));
|
||||
a.addLast(new Integer(1));
|
||||
a.addLast(new Integer(1));
|
||||
a.addLast(new Integer(2));
|
||||
a.addLast(new Integer(2));
|
||||
a.addLast(new Integer(2));
|
||||
a.addLast(new Integer(3));
|
||||
a.addLast(new Integer(4));
|
||||
a.addLast(new Integer(4));
|
||||
a.addLast(new Integer(4));
|
||||
a.addLast(new Integer(4));
|
||||
a.addLast(new Integer(4));
|
||||
|
||||
|
||||
global.cancellaDuplicati(a);
|
||||
|
||||
|
||||
System.out.print(a);
|
||||
a.reverse();
|
||||
System.out.print(a);
|
||||
|
||||
Iterator<test_object> it = a.iterator();
|
||||
System.out.print(it.hasNext());
|
||||
System.out.print(it.next());
|
||||
System.out.print(it.hasNext());
|
||||
System.out.print(it.next());
|
||||
System.out.print(it.hasNext());
|
||||
System.out.print(it.next());
|
||||
System.out.print(it.hasNext());
|
||||
System.out.print(it.next());
|
||||
System.out.print(it.hasNext());
|
||||
|
||||
Iterator<Position<test_object>> itp = a.positions();
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
System.out.print(itp.next().element());
|
||||
System.out.print(itp.hasNext());
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
52
com/xgiovio/UnsortedListPriorityQueueTest.java
Normal file
52
com/xgiovio/UnsortedListPriorityQueueTest.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package com.xgiovio;
|
||||
|
||||
import priorityqueue.SortedListPriorityQueue;
|
||||
import priorityqueue.UnsortedListPriorityQueue;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 02/04/2014
|
||||
* Time: 00:10
|
||||
*/
|
||||
public class UnsortedListPriorityQueueTest {
|
||||
|
||||
public static void main(String[] args) throws InvalidKeyException{
|
||||
|
||||
|
||||
UnsortedListPriorityQueue<Integer,String> a = new UnsortedListPriorityQueue<Integer, String>();
|
||||
a.insert(5,"hello");
|
||||
System.out.println(a.size());
|
||||
System.out.println(a);
|
||||
a.insert(11,"sdas");
|
||||
System.out.println(a.size());
|
||||
|
||||
a.insert(21,"sdas");
|
||||
a.insert(1,"sdas");
|
||||
a.insert(-20,"slkjldas");
|
||||
|
||||
System.out.println(a);
|
||||
System.out.println(a.size());
|
||||
a.insert(21,"sdas");
|
||||
a.insert(-34,"sdas");
|
||||
a.insert(45,"slkjldas");
|
||||
System.out.println(a);
|
||||
System.out.println(a.size());
|
||||
|
||||
System.out.println(a.min());
|
||||
|
||||
System.out.println(a.size());
|
||||
a.removeMin();
|
||||
System.out.println(a.size());
|
||||
System.out.println(a);
|
||||
System.out.println(a.min());
|
||||
a.removeMin();
|
||||
System.out.println(a);
|
||||
System.out.println(a.size());
|
||||
System.out.println(a.min());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
package com.xgiovio;
|
||||
|
||||
import position.Position;
|
||||
import position.PositionList;
|
||||
import sequence.Sequence;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
@@ -19,4 +24,35 @@ class global {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void cancellaDuplicati (PositionList<Integer>L){
|
||||
|
||||
if (L.size() >=2){
|
||||
Position<Integer> first;
|
||||
Position<Integer> second;
|
||||
first = L.first();
|
||||
second = L.next(first);
|
||||
for (;true;) {
|
||||
if (first.element().equals(second.element())) {
|
||||
L.remove(second);
|
||||
if (first!= L.last()) {
|
||||
second = L.next(first);
|
||||
}else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
first = second;
|
||||
if (first!= L.last()) {
|
||||
second = L.next(second);
|
||||
}else {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
catch (ClassCastException err){
|
||||
throw new InvalidKeyException();
|
||||
@@ -78,13 +80,30 @@ 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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user