/**
* Returns an iterator of vertices adjacent to v.
* Time complexity for iterating over all vertices: O(n),
* where n is the number of adjacent vertices.
*
* @param v vertex.
* @return an iterator of vertices adjacent to v.
* @throws IllegalArgumentException if v is out of range.
*/
public VertexIterator adjacentVertices(int v) {
if(v < 0 || v >= numVertices) {
throw new IllegalArgumentException("Out of range");
}
return new AdjacentVerticesIterator(v);
}
/**
* Testar iteratorn
*/
public void test(int v) {
VertexIterator test = adjacentVertices(v);
System.out.println(test.next());
}
private class AdjacentVerticesIterator implements VertexIterator {
int nextPos = 0;
int vertex;
AdjacentVerticesIterator(int v) {
vertex = v;
}
// private void findNext() {
// nextPos++;
// while (nextPos < numVertices && adjacentVertices[vertex] == null)
// nextPos++;
// }
public boolean hasNext() {
return nextPos < adjacentVertices[vertex].size();
}
public int next() {
int pos = nextPos;
if (pos >= adjacentVertices[vertex].size()) {
throw new NoSuchElementException("This iterator has no more elements.");
}
pos++;
return pos; //return the element at this position. but how ?
}
}