0% found this document useful (0 votes)
2 views19 pages

Node Methods (1)

The document contains a series of Java methods for manipulating a linked list data structure, including operations such as summing values, counting occurrences, deleting nodes, and checking for palindromes. It also includes methods for inserting, moving, and splitting nodes, as well as checking for equality and removing duplicates. Each method is designed to handle generic types, allowing for flexibility in the data stored within the nodes.

Uploaded by

lollmaolmaolol04
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
2 views19 pages

Node Methods (1)

The document contains a series of Java methods for manipulating a linked list data structure, including operations such as summing values, counting occurrences, deleting nodes, and checking for palindromes. It also includes methods for inserting, moving, and splitting nodes, as well as checking for equality and removing duplicates. Each method is designed to handle generic types, allowing for flexibility in the data stored within the nodes.

Uploaded by

lollmaolmaolol04
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 19

// sums up the values in the Node

public static Double sumN(Node<Double> lst){


if(lst == null) return null;
Node<Double> x = lst;
double sum = 0;
while(x!=null){
sum+=x.getValue();
x = x.getNext();
}
return sum;
}
//returns the size of the Node for example x = {1, 2} so the size is 2
public static <T> int sizeN(Node<T> head) {
int size = 0;
Node<T> current = head;

while (current != null) {


size++;
current = current.getNext();
}

return size;
}

//checks if y is a sub Node of x


public static <T> boolean subN(Node<T> x, Node<T> y) {
if (x == null || y == null) {
return false;
}
Node<T> currentX = x;
Node<T> currentY = y;

while (currentX != null) {


if (currentX.getValue().equals(currentY.getValue())) {
Node<T> tempX = currentX.getNext();
Node<T> tempY = currentY.getNext();
while (tempX != null && tempY != null &&
tempX.getValue().equals(tempY.getValue())) {
tempX = tempX.getNext();
tempY = tempY.getNext();
}

if (tempY == null)return true;

}
currentX = currentX.getNext();
}
return false;
}

// checks if val exists in head


public static <T> boolean containsElementN(Node<T> head, T val) {
Node<T> current = head;
while (current != null) {
if (current.getValue().equals(target))return true;
current = current.getNext();
}
return false;
}
// counts how many times val is in Head
public static <T> int countOccurrencesN(Node<T> head, T val) {
int count = 0;
Node<T> current = head;
while (current != null) {
if (current.getValue().equals(val))count++;
current = current.getNext();
}
return count;
}

// deletes index of a Node, Node head = {1, 2, 3, 4}; deleteN(head, 0)


results in head = {2, 3, 4}
public static <T> void deleteN(Node<T> head, int index) {
if (index < 0) {
return;
}

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 =

public static <T> T getN(Node<T> head, int index) {


Node<T> current = head;
for (int i = 0; i < index && current != null; i++)
current = current.getNext();
if (current == null)return null;
return current.getValue();
}

0, 0) ⇒ head = {0, 2, 3}
// sets value at a specific index of a Node, head = {1, 2, 3}; setN(head,

public static <T> void setN(Node<T> head, int index, T value) {


if (index < 0)return;
Node<T> current = head;
for (int i = 0; i < index && current != null; i++)
current = current.getNext();
if (current == null)return;
current.setValue(value);
}
// checks if a Node is a palindrome a word, phrase, or sequence that
reads the same backward as forward
public static <T> boolean isPoli(Node<T> x){
if(x == null) return true;
int s = sizeN(x)/2;
Node<T> y = x;
while (s>0){
y = y.getNext();
s-=1;
}
y = reverseN(y);
while (y != null){
if(y.getValue()!=x.getValue()) return false;
y = y.getNext();
x = x.getNext();
}
return true;
}

// reverses a Node with getN, setN, sizeN


public static <T> void reverseN(Node<T> head) {
int start = 0;
int end = sizeN(head) - 1;

while (start < end) {


T x = getN(head, start);
setN(head, start, getN(head, end));
setN(head, end, x);

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;
}

how to insert at index 0:


insertN(Node, 1, getN(x, 0));
setN(x, 0, value to insert at index 0);
// inserts a value to a Node at a specific index
X = {1, 2, 3, 4, 5}; insert(x, 1, 4) ⇒ x = {1, 4, 2, 3, 4, 1}
public static <T> void insertN(Node<T> head, int index, T value) {
Node<T> newNode = new Node<>(value);

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;

while (x2 != null) {


if (x2.getValue().equals(val)) {
if (z == null) {
z = new Node<>(count, null);
tail = z;
} else {
Node<Integer> newNode = new Node<>(count, null);
tail.setNext(newNode);
tail = newNode;
}
}
x2 = x2.getNext();
count++;
}

return z;
}

// removes every of the None first Occurrences of any value in a Node,


X = {1, 2, 3, 4, 1} removeNonFirstOccurrencesN(x) results in x = {1, 2,
3, 4}
public static <T >void removeNonFirstOccurrencesN(Node<T> head) {
if (head == null) return;

Node<T> current = head;

while (current != null) {


Node<T> runner = current;

while (runner.getNext() != null) {


if (runner.getNext().getValue().equals(current.getValue())) {
runner.setNext(runner.getNext().getNext());
} else runner = runner.getNext();
}
current = current.getNext();
}
}
// removes only the none first consecutive occurrences of every value
in a Node
After using the method on x = {1, 1, 2, 3, 2, 2, 1}, x will be {1, 2, 3, 2,
1}
public static <T> void removeConsecutiveOccurrences(Node<T>
head) { // can change data type
if (head == null || !head.hasNext()) return;

Node<T> prev = head;


Node<T> current = head.getNext();

while (current != null) {


if (current.getValue().equals(prev.getValue())) {
prev.setNext(current.getNext());
current = current.getNext();
} else {
prev = current;
current = current.getNext();
}
}
}
//moves everything between start and end including start and end to
index 0(uses set, get, insert, delete),
x = {1, 2, 3, 4}; moveTo(x, 1, 2, 0) – indexes 1-2 = {2, 3}
results in x = {2, 3, 1, 4}
public static <T> void moveTo0(Node<T> head, int start, int end, int
newStart){
T x;
T x0 = head.getValue();
T y;
int z = 0;
x = head.getValue();
y = getN(head, start);
head.setValue(y);
if(start+1 == end) {
x = head.getValue();
moveTo(head, start, end, 1);
deleteN(head, 0);
insertN(head, end,x0);
}else {
moveTo(head, start+1, end, 1);
setN(head, end, x);
}
}

// 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;

Node<T> startPrev = null;


Node<T> startNode = head;
for (int i = 0; i < start; i++) {
startPrev = startNode;
startNode = startNode.getNext();
}
Node<T> endNode = startNode;
for (int i = start; i < end; i++) {
endNode = endNode.getNext();
}
Node<T> afterEndNode = endNode.getNext();

if (startPrev != null) {
startPrev.setNext(afterEndNode);
} else {
head = afterEndNode;
}

Node<T> newStartPrev = null;


Node<T> newNode = head;
for (int i = 0; i < newStart; i++) {
newStartPrev = newNode;
newNode = newNode.getNext();
}
if (newStartPrev != null) {
newStartPrev.setNext(startNode);
} else {
head = startNode;
}
endNode.setNext(newNode);
}
//deletes every index of a Node at which there is a specific value at.
public static <T> void deleteNT(Node<T> head, T value) {
for(int i = 0; i<sizeN(head); i++){
if(getN(head, i) == value) {
deleteN(head, i);
i--;
}
}
}

// creates a Node which is the sorted version of head


public static Node<Integer> sortN(Node<Integer> head) {
if (head == null || !head.hasNext()) return head;

Node<Integer> sortedHead = null;

while (head != null) {


Node<Integer> current = head;
head = head.getNext();
if (sortedHead == null || current.getValue() <
sortedHead.getValue()) {
current.setNext(sortedHead);
sortedHead = current;
} else {
Node<Integer> sortedCurrent = sortedHead;
while (sortedCurrent.getNext() != null &&
sortedCurrent.getNext().getValue() < current.getValue()) {
sortedCurrent = sortedCurrent.getNext();
}
current.setNext(sortedCurrent.getNext());
sortedCurrent.setNext(current);
}
}
return sortedHead;
}

// splits a Node into a Node of Nodes with the sizeN of n,


Node<Integer> x = {1, 2, 3, 4, 5}, Node<Node<Integer>> y = split(x,
4)
results in y = {{1, 2}, {3}, {4}, {5}}
Another example: Node<Integer> x = {1, 2, 3, 4, 5};
Node<Node<Integer>> y = split(x, 2); results in y = {{1, 2, 3}, {4,
5}}
public static <T> Node<Node<T>> splitN(Node<T> head, int n) {
if (head == null || n <= 0) return null;
int size = sizeN(head);
int partSize = size / n;
int remainder = size % n;

Node<Node<T>> resultHead = null;


Node<Node<T>> resultTail = null;

Node<T> current = head;


for (int i = 0; i < n; i++) {
int currentPartSize = partSize + (i < remainder ? 1 : 0);
Node<T> partHead = current;
Node<T> partTail = current;

for (int j = 1; j < currentPartSize; j++) partTail =


partTail.getNext();

Node<T> nextPartHead = partTail.getNext();


partTail.setNext(null);

Node<Node<T>> newNode = new Node<>(partHead);


if (resultHead == null) resultHead = newNode;
else resultTail.setNext(newNode);

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;

Node<Node<T>> resultHead = null;


Node<Node<T>> currentResult = null;

Node<T> current = head;


int currentIndex = 0;
Node<T> tempHead = null;
Node<T> tempTail = null;

while (current != null && currentIndex < start) {


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 (resultHead == null) {
resultHead = new Node<>(tempHead);
currentResult = resultHead;
} else {
currentResult.setNext(new Node<>(tempHead));
currentResult = currentResult.getNext();
}
}

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");

int min = head.getValue();


Node<Integer> current = head.getNext();

while (current != null) {


if (current.getValue() < min) min = current.getValue();

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");

int max = head.getValue();


Node<Integer> current = head.getNext();

while (current != null) {


if (current.getValue() > max) max = current.getValue();

current = current.getNext();
}
return max;
}

how to create a Node:


Node<data type> x = new Node<>(value of data type, new
Node<>( value of data type, new Node<>( value of data type, new
Node<>( value of data type, new Node<>( value of data type)))));
this translates to:
Node<Integer> x = new Node<>(1, new Node<>(2, new Node<>(3,
new Node<>(4, new Node<>(1)))));
How to run on a Node using sizeN and getN:
for (int i = 0; i < sizeN(x); i++) {
System.out.println(getN(x, i));
}

the class Node:


public T getValue() {
return this.value;
}

public void setValue(T x) {


this.value = x;
}

public Node<T> getNext() {


return this.next;
}

public void setNext(Node<T> next) {


this.next = next;
}

public boolean hasNext() {


return this.next != null;
}
public String toString() {
return this.value.toString();
}

Magnitude of running time, order of DE via power of n


1. sumN = O(n)
2. sizeN = O(n)
3. subN = O(n^2)
4. containsElementN = O(n)
5. countOccurrencesN = O(n)
6. deleteN = O(n)
7. getN = O(n)
8. setN = O(n)
9. isPoli = O(n)
10. reverseVN = O(n^2)
11. reverseN = O(n)
12. insertN = O(n)
13. isEqualNode = O(n)
14. OcValN = O(n)
15. removeNoneFirstOccurrencesN = O(n^2)
16. removeConsecutiveOccurrences = O(n)
17. moveTo0 = O(n)
18. moveTo = O(n)
19. deleteN = O(n^2)
20. sortN = O(n^2)
21. splitN = O(n)
22. CSplitN = O(n)
23. findMinN = O(n)
24. findMaxN = O(n)

You might also like