Node Methods (1)
Node Methods (1)
return size;
}
}
currentX = currentX.getNext();
}
return false;
}
if (index == 0) {
if (head.getNext() != null) {
head.setValue(head.getNext().getValue());
head.setNext(head.getNext().getNext());
} else head.setValue(null);
return;
}
Node<T> current = head;
Node<T> prev = null;
for (int i = 0; i < index && current != null; i++) {
prev = current;
current = current.getNext();
}
if (current == null)return;
prev.setNext(current.getNext());
}
getN(head, 0) ⇒ value = 1
// gets value at a specific index of a Node, head = {1, 2, 3}; T value =
0, 0) ⇒ head = {0, 2, 3}
// sets value at a specific index of a Node, head = {1, 2, 3}; setN(head,
start++;
end--;
}
}
// creates a reversed version of a Node, Node<data type x> n =
{R(any value of data type x)}
Node<x> y = reverseN(n) = reversed version of n
public static <T> Node<T> reverseN(Node<T> n) {
Node<T> temp = new Node<T>(n.getValue());
n = n.getNext();
while (n.hasNext()) {
Node<T> temp2 = new Node<T>(n.getValue(), temp);
n = n.getNext();
temp = temp2;
}
Node<T> newN = new Node<T>(n.getValue(), temp);
return newN;
}
if (index == 0) {
newNode.setNext(head);
head = newNode;
return;
}
Node<T> current = head;
for (int i = 0; i < index - 1 && current.getNext() != null; i++)
current = current.getNext();
if (current.getNext() == null)current.setNext(newNode);
else {
newNode.setNext(current.getNext());
current.setNext(newNode);
}
}
// checks if two Nodes are equal to eachother, x = {1, 2, 3};
y = {1, 2, 3}; isEqualNode(x, y) ⇒ true
public static boolean isEqualNode(Node<String> lst1, Node<String>
lt2) {
if (sizeN(lst1) != sizeN(lt2))
return false;
Node<String> pos1 = lst1;
Node<String> pos2 = lt2;
while (pos1 != null) {
if (!pos1.getValue().equals(pos2.getValue())) return false;
pos1 = pos1.getNext();
pos2 = pos2.getNext();
}
return true;
}
// creates a Node of integers with the indexes of which there is val in x,
x = {1, 2, 3, 4, 1}; Node<Integer> y = OcValN(x, 1)⇒ {0, 4}
public static <T> Node<Integer> OcValN(Node<T> x, T val) {
Node<Integer> z = null;
Node<Integer> tail = null;
Node<T> x2 = x;
int count = 0;
return z;
}
// moves all the elements between start and end including start and
end to the index of newStart and moves the element in newStart to
after the element of end,
Node x = {1, 2, 3, 4, 5, 6}; moveTo(x, 1, 3, 0) // can’t move to
anywhere between
1=<newStart<3
Here we chose the indexes 1-3 which is {2, 3, 4} to move to index 0
which is {1}
So after using the method x will be: x = {2, 3, 4, 1, 5, 6}
Let x = {1, 2, 3, 4, 5, 6}
To move the chosen indexes to the last index the newStart should be
equal to : ((sizeN(x)-1)-((end)- (start))) which in our case equal to 3
so to move {2, 3, 4} to the last index the operation will look like this
moveTo(x, 1, 3, 3) it’ll result in
x = {1, 5, 6, 2, 3, 4}
public static <T> void moveTo(Node<T> head, int start, int end, int
newStart) {
if (start == end || start == newStart) return;
if (startPrev != null) {
startPrev.setNext(afterEndNode);
} else {
head = afterEndNode;
}
resultTail = newNode;
current = nextPartHead;
}
return resultHead;
}
//The method takes the selected Node(head) and will create a new
splited Node out of it according the the chosen start and end. for
example:
A = {1, 2, 3, 4, 5, 6} and B = CSplitN(A, 1, 3) so it'll result in B = {{1},
{2, 3, 4}, {5 ,6}} because {2, 3, 4} are at indexes 1-3, 2 is at index 1
and 4 is at index 3
B is a Node<Node<Integer>> in this example.
public static <T> Node<Node<T>> CSplitN(Node<T> head, int start,
int end) {
if (head == null || start > end) return null;
tempHead = null;
tempTail = null;
while (current != null && currentIndex <= end) {
if (tempHead == null) {
tempHead = new Node<T>(current.getValue());
tempTail = tempHead;
} else {
tempTail.setNext(new Node<T>(current.getValue()));
tempTail = tempTail.getNext();
}
current = current.getNext();
currentIndex++;
}
if (tempHead != null) {
if (currentResult == null) {
resultHead = new Node<>(tempHead);
currentResult = resultHead;
} else {
currentResult.setNext(new Node<>(tempHead));
currentResult = currentResult.getNext();
}
}
tempHead = null;
tempTail = null;
while (current != null) {
if (tempHead == null) {
tempHead = new Node<T>(current.getValue());
tempTail = tempHead;
} else {
tempTail.setNext(new Node<T>(current.getValue()));
tempTail = tempTail.getNext();
}
current = current.getNext();
}
if (tempHead != null) {
if (currentResult == null) {
resultHead = new Node<>(tempHead);
} else {
currentResult.setNext(new Node<>(tempHead));
}
}
return resultHead;
}
//finds the min value in a Node
public static int findMinN(Node<Integer> head) {
if (head == null) throw new IllegalArgumentException("The list
cannot be empty");
current = current.getNext();
}
return min;
}
//finds the max value in a Node
public static int findMaxN(Node<Integer> head) {
if (head == null) throw new IllegalArgumentException("The list
cannot be empty");
current = current.getNext();
}
return max;
}