My method "addEdge" adds a node to the adjacency list of the selected node, but I want the method to add the selected node to its adjacency list as well and turn it into an undirected graph.
public class Main {
public static void main(String[] args) {
Graph g = new Graph();
Node n1 = new Node("A");
Node n2 = new Node("B");
Node n3 = new Node("C");
Node n4 = new Node("D");
n1.addEdge(n2);
n2.addEdge(n3);
n3.addEdge(n4);
n4.addEdge(n1);
}
}
class Node{
String node;
boolean isVisited;
LinkedList<Node> edge;
Node(String node){
this.node = node;
edge = new LinkedList<>();
}
public void addEdge(Node node){
edge.add(node);
}
public LinkedList<Node> getEdge(){
return edge;
}
}
Suggest rather than
n1.addEdge(n2);
you do
g.addEdge( n1, n2 );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.