Graphen/Graph.java

51 lines
1.2 KiB
Java

import java.util.ArrayList;
public class Graph
{
private ArrayList<Node> nodes;
private ArrayList<Edge> edges;
public Graph(){
this.nodes = new ArrayList<Node> ();
this.edges = new ArrayList<Edge>();
}
public void addNode(Node n) {
this.nodes.add(n);
}
public void addNode(int nr){
this.nodes.add(new Node(nr));
}
public void addEdge(Edge e){
this.edges.add(e);
e.getAnfang().addEdge(e);
e.getEnde().addEdge(e);
}
public void addEdge(int a, int e, int w, boolean g){
Node an = null;
Node en = null;
for (int i = 0; i < this.nodes.size(); i++){
if (this.nodes.get(i).getNumber() == a){
an = this.nodes.get(i);
}
if (this.nodes.get(i).getNumber()== e){
en = this.nodes.get(i);
}
}
if (an == null || en == null){
return;
}
this.addEdge(new Edge(an, en, w, g));
}
public Node getNode(int nr){
for (Node n : this.nodes){
if (n.getNumber() == nr){
return n;
}
}
return null;
}
}