Results 1 to 1 of 1
  1. #1
    whizzz is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default (Help) Hashtable for adjacency list

    hey people,
    i have a little problem about implementing a Hashtable in my project.
    i want to store nodes and the edges between them in the hashtable.

    Java Code:
    Map<City, List<City>> adj= new HashMap<City, List<City>>();
    ...
    // add a connection from -> to
    public boolean add(City from, City to) {
       List<City> tos= adj.get(from);
       if (tos == null) // from is not in the map
          return false;
       tos.add(to); // add a new to 
       return true;
    }
    // add a single airport
    public boolean add(City airport) {
       List<City> tos= adj.get(airport);
       if (tos != null) // already in the list
          return false;
       tos= new ArrayList<City>();
       adj.put(airport, tos); // add airport and a new empty adj. list
       return true;
    }
    the example of "Josah" is really close but my problem occurs when i want to know the predecessors or successors of selected nodes(Cities in this case).
    for example i want to implement my edges like this;
    p and s are Cities and im adding and edge from p city to s city
    p-->s
    p.addsuccessor(s);
    s.addpredecessor(p);

    so for the methods of "addpredecessor" and "addsuccesors" , how can i use the hashtable and should i use 2 different lists for 2 different methods ?
    in that case, how its possible to connect a node (City) to 2 different lists(predecessors, successors) in Hastable? so i can call the selected Cities predecessors or successors any time from one Hashtable.
    thanks.
    Last edited by whizzz; 10-21-2010 at 12:56 PM.

Similar Threads

  1. Implementing an Adjacency List for a Flight Map
    By javaRancher2009 in forum New To Java
    Replies: 3
    Last Post: 12-07-2009, 03:45 PM
  2. hashtable
    By vijayabaskar in forum Java Servlet
    Replies: 0
    Last Post: 04-06-2009, 08:20 AM
  3. hashtable
    By vijayabaskar in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 08:05 AM
  4. Hashtable
    By angelicsign in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 04:30 PM
  5. Hashtable example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 08:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •