From da0e70ef00819bc847b44fc99ea84b935a09799c Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Tue, 20 Oct 2015 14:14:52 +0200 Subject: First version practical 1 (garbage collection) --- Practical1/src/nl/camilstaps/cs/graphs/Graph.java | 68 +++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Practical1/src/nl/camilstaps/cs/graphs/Graph.java (limited to 'Practical1/src/nl/camilstaps/cs/graphs/Graph.java') diff --git a/Practical1/src/nl/camilstaps/cs/graphs/Graph.java b/Practical1/src/nl/camilstaps/cs/graphs/Graph.java new file mode 100644 index 0000000..ef89258 --- /dev/null +++ b/Practical1/src/nl/camilstaps/cs/graphs/Graph.java @@ -0,0 +1,68 @@ +package nl.camilstaps.cs.graphs; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * A Graph is a list of Nodes. + * + * @author Camil Staps + */ +public class Graph { + protected final List nodes; + + public Graph() { + this.nodes = new ArrayList<>(); + } + + /** + * Create a copy of another Graph, also copying all the Nodes + * @param g + */ + public Graph(Graph g) { + this(); + this.nodes.addAll(g.nodes.stream().map(Node::new).collect(Collectors.toList())); + } + + /** + * Add a Node + * @param node + */ + public void addNode(Node node) { + this.nodes.add(node); + } + + /** + * Get a Node + * @param i + * @return + */ + public Node getNode(int i) { + return this.nodes.get(i); + } + + /** + * Remove a node + * @param n + */ + public void removeNode(Node n) { + nodes.remove(n); + } + + /** + * Remove a list of nodes, and update all references + * @param ns + */ + public void removeNodes(Collection ns) { + nodes.removeAll(ns); + } + + public String toString() { + StringBuilder sb = new StringBuilder("Graph:"); + for (Node n : nodes) { + sb.append("\n ").append(n).append(": "); + for (Node n2 : n.getNeighbours()) sb.append(" - ").append(n2); + } + return sb.toString(); + } +} -- cgit v1.2.3