I am currently learning about data structures (eg LinkedList, DoublyLinkedList, ArrayList,...) and I was wondering how to implement a (not directed) graph in Java.
I was thinking about two classes: Graph
and Node<T>
Each node should know to which other nodes it is connected (is a List<Node<T>>
appropriate? What kind of List would be best?)
The Graph
class could then provide methods like boolean contains(T element)
The Node
class would have no other use so how do I restrict visibility so that only Graph
has access?
EDIT: furthermore how can I weigh the connections between nodes? I guess I would need a completely different Implementation than mentioned above since a simple List of connected nodes would not be enough?