Java Reference
In-Depth Information
protected Edge(VertexInterface<T> endVertex, double edgeWeight)
{
vertex = endVertex;
weight = edgeWeight;
} // end constructor
protected VertexInterface<T> getEndVertex()
{
return vertex;
} // end getEndVertex
protected double getWeight()
{
return weight;
} // end getWeight
} // end Edge
Note: An instance of the inner class Edge contains both the vertex that ends it and the
edge's weight, if any. Although not necessary for unweighted graphs, Edge allows us to use
one class of vertices for both weighted and unweighted graphs.
Implementing the Class Vertex
29.11
An outline of the class. To hide Vertex from the clients of the graph, we place it within the pack-
age GraphPackage that we introduced in the previous chapter. Listing 29-3 outlines the class and
shows its data fields and constructor. An ADT list that has an iterator serves as the adjacency list
edgeList . We have chosen the linked implementation LinkedListWithIterator discussed in
Segment 15.19 of Chapter 15.
LISTING 29-3
An outline of the class Vertex
package GraphPackage;
import java.util.Iterator;
import java.util.NoSuchElementException;
import ADTPackage.*; // classes that implement various ADTs
class Vertex<T> implements VertexInterface<T>
{
private T label;
private ListWithIteratorInterface<Edge> edgeList; // edges to
// neighbors
private boolean visited; // true if visited
private VertexInterface<T> previousVertex; // on path to this vertex
private double cost;
// of path to this vertex
public Vertex(T vertexLabel)
{
label = vertexLabel;
edgeList = new LinkedListWithIterator<Edge>();
 
 
Search WWH ::




Custom Search