Java Reference
In-Depth Information
public void remove()
{
throw new UnsupportedOperationException();
} // end remove
} // end neighborIterator
In a similar manner, the method getWeightIterator returns an instance of a private inner
class weightIterator . This class is similar to the class neighborIterator .
29.14
The methods hasNeighbor and getUnvisitedNeighbor . The method hasNeighbor uses the
method isEmpty of LinkedListWithIterator to test whether edgeList is empty:
public boolean hasNeighbor()
{
return !edgeList.isEmpty();
} // end hasNeighbor
Using the iterator returned by getNeighborIterator , the method getUnvisitedNeighbor
returns an adjacent vertex that is unvisited. This task is necessary in a topological sort.
public VertexInterface<T> getUnvisitedNeighbor()
{
VertexInterface<T> result = null ;
Iterator<VertexInterface<T>> neighbors = getNeighborIterator();
while ( (neighbors.hasNext() && (result == null ) )
{
VertexInterface<T> nextNeighbor = neighbors.next();
if (!nextNeighbor.isVisited())
result = nextNeighbor;
} // end while
return result;
} // end getUnvisitedNeighbor
29.15
The remaining methods. Vertex should override the method equals . Two vertices are equal if
their labels are equal.
public boolean equals(Object other)
{
boolean result;
if ((other == null ) || (getClass() != other.getClass()))
result = false ;
else
{
Vertex<T> otherVertex = (Vertex<T>)other;
result = label.equals(otherVertex.label);
} // end if
return result;
} // end equals
The remaining methods of Vertex have uncomplicated implementations and are left as exercises.
Question 3 Given the interface VertexInterface and the class Vertex , write Java statements
that create the vertices and edges for the following directed, weighted graph. This graph contains
three vertices— A , B , and C —and four edges, as follows: A
B , B
C , C
A , A
C . These
edges have the weights 2, 3, 4, and 5, respectively.
Search WWH ::




Custom Search