Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,27 @@
* @author Taras Boychuk
*/
public class Node<T> {
// todo:
T element;
Node<T> reference;

public Node(T element) {
this.element =
element;
}

public T getElement() {
return element;
}

public void setElement(T element) {
this.element = element;
}

public Node<T> getReference() {
return reference;
}

public void setReference(Node<T> reference) {
this.reference = reference;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
* <p><p>
Expand All @@ -22,7 +25,7 @@ private Nodes() {
* @return a new instance of {@link Node}
*/
public static <T> Node<T> create(T element) {
throw new ExerciseNotCompletedException(); // todo:
return new Node<>(element);
}

/**
Expand All @@ -33,7 +36,7 @@ public static <T> Node<T> create(T element) {
* @param <T> a genetic type
*/
public static <T> void link(Node<T> first, Node<T> second) {
throw new ExerciseNotCompletedException(); // todo:
first.reference = second;
}

/**
Expand All @@ -46,7 +49,10 @@ public static <T> void link(Node<T> first, Node<T> second) {
* @return a reference to a first node created based on firstElement
*/
public static <T> Node<T> pairOf(T firstElement, T secondElement) {
throw new ExerciseNotCompletedException(); // todo:
Node<T> firstNode = new Node<>(firstElement);
firstNode.reference = new Node<>(secondElement);

return firstNode;
}

/**
Expand All @@ -60,7 +66,13 @@ public static <T> Node<T> pairOf(T firstElement, T secondElement) {
* @return a reference to the first node
*/
public static <T> Node<T> closedPairOf(T firstElement, T secondElement) {
throw new ExerciseNotCompletedException(); // todo:
Node<T> firstNode = new Node<>(firstElement);
Node<T> secondNode = new Node<>(secondElement);

firstNode.reference = secondNode;
secondNode.reference = firstNode;

return firstNode;
}

/**
Expand All @@ -72,7 +84,13 @@ public static <T> Node<T> closedPairOf(T firstElement, T secondElement) {
* @return a reference to the first element of the chain
*/
public static <T> Node<T> chainOf(T... elements) {
throw new ExerciseNotCompletedException(); // todo:
Node<T> firstNode = new Node<>(elements[0]);
Node<T> current = firstNode;
for (int i = 1; i < elements.length; i++) {
current.reference = new Node<>(elements[i]);
current = current.reference;
}
return firstNode;
}

/**
Expand All @@ -85,6 +103,16 @@ public static <T> Node<T> chainOf(T... elements) {
* @return a reference to the first element of the chain
*/
public static <T> Node<T> circleOf(T... elements) {
throw new ExerciseNotCompletedException(); // todo:
Node<T> firstNode = new Node<>(elements[0]);
Node<T> 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;
}
}