import java.util.Hashtable; import java.util.Enumeration; /** Class representing an undirected graph composed * of nodes. The node class is a top-level class * nested within the Graph1 class. */ public class Graph1 { private Hashtable nodeList = new Hashtable(); public void addNode( int x, int y ) { Node n = new Node( x, y ); if ( ! nodeList.containsKey( n.key() ) ) { nodeList.put( n.key(), n ); } } public String toString() { StringBuffer sb = new StringBuffer( "[ " ); Enumeration e = nodeList.elements(); while ( e.hasMoreElements() ) { sb.append( e.nextElement().toString() + " " ); } sb.append( "]" ); return sb.toString(); } public static void main( String[] args ) { System.out.println( "creating the graph" ); Graph1 g = new Graph1(); System.out.println( "adding nodes" ); g.addNode( 4, 5 ); g.addNode( -6, 11 ); System.out.println( g ); } /** The class representing nodes within the graph */ private static class Node { private int x, y; public Node( int x, int y ) { this.x = x; this.y = y; } public Object key() { return x + "," + y; } public String toString() { return "(" + x + "," + y + ")"; } } // end of Node class }