Fixato bug e implementato alcune funzioni.
This commit is contained in:
@@ -1,74 +1,34 @@
|
||||
package iterator;
|
||||
|
||||
import exceptions.BoundaryViolationException;
|
||||
import position.Position;
|
||||
import position.PositionList;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 06/04/2014
|
||||
* Time: 21:06
|
||||
*/
|
||||
|
||||
//iterator implementation with cursor for PositionList data structures.
|
||||
|
||||
public class ElementIterator<E> implements Iterator<E> {
|
||||
protected PositionList<E> list; // the underlying list
|
||||
protected Position<E> cursor; // the next position
|
||||
|
||||
public ElementIterator (PositionList<E> structure){
|
||||
public ElementIterator(PositionList<E> L) {
|
||||
list = L;
|
||||
cursor = (list.isEmpty())? null : list.first();
|
||||
}
|
||||
|
||||
new_structure = structure;
|
||||
public boolean hasNext() { return (cursor != null); }
|
||||
|
||||
}
|
||||
public E next() throws NoSuchElementException {
|
||||
if (cursor == null)
|
||||
throw new NoSuchElementException("No next element");
|
||||
E toReturn = cursor.element();
|
||||
cursor = (cursor == list.last())? null : list.next(cursor);
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (pos == null){
|
||||
if (new_structure. size() <= 0){
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
public void remove() throws UnsupportedOperationException {
|
||||
throw new UnsupportedOperationException("remove");
|
||||
}
|
||||
|
||||
} else {
|
||||
}
|
||||
|
||||
try {
|
||||
new_structure.next(pos);
|
||||
return true;
|
||||
}
|
||||
catch (BoundaryViolationException err){
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() throws NoSuchElementException {
|
||||
if (hasNext()){
|
||||
if (pos == null){
|
||||
pos = new_structure.first();
|
||||
}else {
|
||||
pos = new_structure.next(pos);
|
||||
}
|
||||
return pos.element();
|
||||
} else{
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException ();
|
||||
}
|
||||
|
||||
PositionList<E> new_structure = null;
|
||||
Position<E> pos = null;
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user