diff --git a/2-0-data-structures-and-algorithms/2-2-1-node/src/main/java/com/bobobode/cs/Node.java b/2-0-data-structures-and-algorithms/2-2-1-node/src/main/java/com/bobobode/cs/Node.java index b01a4acfb..4282409c3 100644 --- a/2-0-data-structures-and-algorithms/2-2-1-node/src/main/java/com/bobobode/cs/Node.java +++ b/2-0-data-structures-and-algorithms/2-2-1-node/src/main/java/com/bobobode/cs/Node.java @@ -9,5 +9,27 @@ * @author Taras Boychuk */ public class Node { - // todo: + T element; + Node reference; + + public Node(T element) { + this.element = + element; + } + + public T getElement() { + return element; + } + + public void setElement(T element) { + this.element = element; + } + + public Node getReference() { + return reference; + } + + public void setReference(Node reference) { + this.reference = reference; + } } diff --git a/2-0-data-structures-and-algorithms/2-2-1-node/src/main/java/com/bobobode/cs/Nodes.java b/2-0-data-structures-and-algorithms/2-2-1-node/src/main/java/com/bobobode/cs/Nodes.java index 404b5114c..3f8faea68 100644 --- a/2-0-data-structures-and-algorithms/2-2-1-node/src/main/java/com/bobobode/cs/Nodes.java +++ b/2-0-data-structures-and-algorithms/2-2-1-node/src/main/java/com/bobobode/cs/Nodes.java @@ -2,6 +2,9 @@ import com.bobocode.util.ExerciseNotCompletedException; +import java.util.ArrayList; +import java.util.List; + /** * A class that consists of static methods only and provides util methods for {@link Node}. *

@@ -22,7 +25,7 @@ private Nodes() { * @return a new instance of {@link Node} */ public static Node create(T element) { - throw new ExerciseNotCompletedException(); // todo: + return new Node<>(element); } /** @@ -33,7 +36,7 @@ public static Node create(T element) { * @param a genetic type */ public static void link(Node first, Node second) { - throw new ExerciseNotCompletedException(); // todo: + first.reference = second; } /** @@ -46,7 +49,10 @@ public static void link(Node first, Node second) { * @return a reference to a first node created based on firstElement */ public static Node pairOf(T firstElement, T secondElement) { - throw new ExerciseNotCompletedException(); // todo: + Node firstNode = new Node<>(firstElement); + firstNode.reference = new Node<>(secondElement); + + return firstNode; } /** @@ -60,7 +66,13 @@ public static Node pairOf(T firstElement, T secondElement) { * @return a reference to the first node */ public static Node closedPairOf(T firstElement, T secondElement) { - throw new ExerciseNotCompletedException(); // todo: + Node firstNode = new Node<>(firstElement); + Node secondNode = new Node<>(secondElement); + + firstNode.reference = secondNode; + secondNode.reference = firstNode; + + return firstNode; } /** @@ -72,7 +84,13 @@ public static Node closedPairOf(T firstElement, T secondElement) { * @return a reference to the first element of the chain */ public static Node chainOf(T... elements) { - throw new ExerciseNotCompletedException(); // todo: + Node firstNode = new Node<>(elements[0]); + Node current = firstNode; + for (int i = 1; i < elements.length; i++) { + current.reference = new Node<>(elements[i]); + current = current.reference; + } + return firstNode; } /** @@ -85,6 +103,16 @@ public static Node chainOf(T... elements) { * @return a reference to the first element of the chain */ public static Node circleOf(T... elements) { - throw new ExerciseNotCompletedException(); // todo: + Node firstNode = new Node<>(elements[0]); + Node current = firstNode; + for (int i = 1; i < elements.length; i++) { + current.reference = new Node<>(elements[i]); + current = current.reference; + + if(i == elements.length -1){ + current.reference = firstNode; + } + } + return firstNode; } }