-
Notifications
You must be signed in to change notification settings - Fork 0
/
DNANode.java
60 lines (55 loc) · 1.68 KB
/
DNANode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Copyright (c) 2015 Pavel Brilliant. All Rights Reserved.
*/
package com.pavelbrilliant;
/**
* <p>
* Project: GolovachCourses - Core Java
*
* @author Pavel Brilliant
* @version 1.0 02 May 2015
*/
public class DNANode {
int value;
DNANode next;
DNANode down;
public DNANode(final int value, final DNANode next, final DNANode down) {
this.value = value;
this.next = next;
this.down = down;
}
public static DNANode buildDNA(int nodes, int levels) {
DNANode[] dnaArr = new DNANode[nodes * levels];
for (int i = dnaArr.length - 1; i >= 0; i--) {
dnaArr[i] = new DNANode(i, (i < dnaArr.length - 1) ? dnaArr[i + 1] : null, (i < dnaArr.length - nodes) ? dnaArr[i + nodes] : null);
}
return dnaArr[0];
}
public static boolean checkDNAStructure(DNANode dnaNode, int nodes, int levels) {
DNANode[] dnaArr = new DNANode[nodes * levels];
for (int i = 0; i < dnaArr.length; i++) {
dnaArr[i] = dnaNode;
if (dnaNode == null) {
return false;
} else {
dnaNode = dnaNode.next;
}
}
if (dnaArr[dnaArr.length - 1].next != null) {
return false;
}
for (int i = dnaArr.length - 2; i >= 0; i--) {
if (dnaArr[i].next != dnaArr[i + 1]) {
return false;
}
if (i < dnaArr.length - nodes) {
if (dnaArr[i].down != dnaArr[i + nodes]) {
return false;
}
} else if (dnaArr[i].down != null) {
return false;
}
}
return true;
}
}