www.pudn.com > campusleader.rar > Vertex.java, change:2012-01-04,size:5101b


package GraphPackage; 
import java.util.Iterator; 
import java.util.NoSuchElementException; 
	class Vertex<T>implements VertexInterface<T>,java.io.Serializable{ 
		private T label; 
		private ListWithIteratorInterface<Edge>edgeList; 
		private boolean visited; 
		private VertexInterface<T> previousVertex; 
		private double cost; 
		public Vertex(T vertexLabel) 
		{ 
			label=vertexLabel; 
			edgeList=new LinkedListWithIterator<Edge>(); 
			visited=false; 
			previousVertex=null; 
			cost=0; 
			//edgeList.add(this,0); 
		} 
		protected class Edge implements java.io.Serializable 
		{ 
			private VertexInterface<T> vertex; 
			private double weight; 
			private int flag; 
			protected Edge(VertexInterface<T>endVertex,double edgeWeight,int edgeflag) 
			{ 
				vertex=endVertex; 
				weight=edgeWeight; 
				flag=edgeflag; 
			} 
			protected VertexInterface<T>getEndVertex() 
			{ 
				return vertex; 
			} 
			protected double getWeight() 
			{ 
				return weight; 
			} 
			protected int getFlag() 
			{ 
				return flag; 
			} 
		} 
		public boolean connect (VertexInterface<T> endVertex,double edgeWeight,int edgeFlag) 
		{ 
			boolean result =false; 
			if(!this.equals(endVertex)) 
			{ 
				Iterator<VertexInterface<T>> neighbors=this.getNeighborIterator(); 
				boolean duplicateEdge=false; 
				while(!duplicateEdge&&neighbors.hasNext()) 
				{ 
					VertexInterface<T>nextNeighbor=neighbors.next(); 
					if(endVertex.equals(nextNeighbor)) 
						duplicateEdge=true; 
				} 
				if(!duplicateEdge) 
				{ 
					edgeList.add(new Edge(endVertex,edgeWeight,edgeFlag)); 
					result=true; 
				} 
			} 
				return result; 
		} 
		public boolean connect (VertexInterface<T>endVertex) 
		{ 
			return connect(endVertex,0,0); 
		} 
		public int getNeighborFlag(VertexInterface<T> endVertex) 
		{ 
			Iterator<Edge> edges=edgeList.getIterator(); 
			Edge next; 
			while(edges.hasNext()) 
			{ 
				next=edges.next(); 
				if(endVertex.getLabel()==next.getEndVertex().getLabel()) 
				{ 
					return next.getFlag(); 
				} 
			} 
			return 0; 
		} 
		public double getNeighborWeight(VertexInterface<T> endVertex) 
		{ 
			Iterator<Edge> edges=edgeList.getIterator(); 
			Edge next; 
			while(edges.hasNext()) 
			{ 
				next=edges.next(); 
				if(endVertex.getLabel()==next.getEndVertex().getLabel()) 
				{ 
					return next.getWeight(); 
				} 
			} 
			return 0; 
		} 
		public Iterator<VertexInterface<T>> getNeighborIterator() 
		{ 
			return new neighborIterator(); 
		} 
		private class neighborIterator implements Iterator<VertexInterface<T>> 
		{ 
			private Iterator<Edge> edges; 
			private neighborIterator() 
			{ 
				edges=edgeList.getIterator(); 
			} 
			public boolean hasNext() 
			{ 
				return edges.hasNext(); 
			} 
			public VertexInterface<T> next() 
			{ 
				VertexInterface<T> nextNeighbor=null; 
				if(edges.hasNext()) 
				{ 
					Edge edgeToNextNeighbor=edges.next(); 
					nextNeighbor=edgeToNextNeighbor.getEndVertex(); 
				} 
				else 
					throw new NoSuchElementException(); 
				return nextNeighbor; 
			} 
			public void remove() 
			{ 
				 
			} 
		} 
		public VertexInterface<T>getUnvisitedNeighbor() 
		{ 
			VertexInterface<T> result=null; 
			Iterator <VertexInterface<T>> neighbors=this.getNeighborIterator(); 
			while((neighbors.hasNext())&&(result==null)) 
			{ 
				VertexInterface<T> nextNeighbor=neighbors.next(); 
				if(!nextNeighbor.isVisited()) 
						result=nextNeighbor; 
			} 
			return result; 
		} 
		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); 
			} 
			return result;                              
		} 
		public boolean isVisited() 
		{ 
			return visited; 
		} 
		public void unVisit() 
		{ 
			visited=false; 
		} 
		public void visit() 
		{ 
			visited=true; 
		} 
		public double getCost() { 
			return cost; 
		} 
		public void setCost(double newCost) { 
			cost=newCost; 
		} 
		public T getLabel() { 
			return label; 
		} 
		public VertexInterface<T> getPredecessor() { 
			return previousVertex; 
		} 
		public boolean hasPredecessor() { 
			return previousVertex!=null; 
		} 
		public Iterator<Double> getWeightIterator() 
		{ 
			return new WeightIterator(); 
		} 
		public class WeightIterator implements Iterator<Double> 
		{ 
			private Iterator<Edge> weights; 
			public WeightIterator() 
			{ 
				weights=edgeList.getIterator(); 
			} 
			public boolean hasNext() 
			{ 
				return weights.hasNext(); 
			} 
			public Double next() 
			{ 
				Double nextWeight=null; 
				if(weights.hasNext()) 
				{ 
					Edge WeightToNextNeighbor=weights.next(); 
					nextWeight=WeightToNextNeighbor.getWeight(); 
				} 
				else  
					throw new NoSuchElementException(); 
				return nextWeight; 
			} 
			public void remove() 
			{ 
				 
			} 
		} 
		public boolean hasNeighbor() { 
			return !edgeList.isEmpty(); 
		} 
		public void setPredecessor(VertexInterface<T> predecessor) { 
			previousVertex=predecessor; 
		} 
}