its urgent a) create a method called sum in class Node ( Node classe given below
ID: 3575094 • Letter: I
Question
its urgent
a) create a method called sum in class Node ( Node classe given below), that will recursively calculate the sum of all of the int data values from that node to the last node in the list. (HINT: using the next pointer) This method should return (NOT PRINT OUT) the sum that was calculated.
b) IN CLASS LINKED LIST, create a method called sum that will take an int, n, as a parameter. It will call method sum (of class Node) on the nth node in the list. (the head is node 1, the node after head is 2, etc.) And then print out to a file the sum that was calculated. If the parameter passed is negative or zero, then the sum should be zero. If n is greater than the number of nodes in the list, then throw an appropriate exception.
LinkedList class
//our linked list will enter data items in sorted order
public class LinkedList1 {
// head is the first node in the LL
private Node1 head = null;
public Node1 getHead() { return head; }
public void setHead(Node1 h) { head = h; }
public LinkedList1(Node1 n) { head = n; }
public LinkedList1() { head = null; }
public void insertNode(Node1 nn) {
if (head == null) { // list is empty
head = nn;
} else {
if (nn.getData() <= head.getData()) {
// insert node at beginning of list
nn.setNext(head);
head = nn;
} else {
Node1 prev = head;
Node1 cur = head.getNext();
while (cur != null) {
if (nn.getData() <= cur.getData()) {
nn.setNext(cur);
prev.setNext(nn);
// make sure once it is added it is not added again
break;
}
prev = cur;
cur = cur.getNext();
}
// if it is add to end of list
if (cur == null) {
prev.setNext(nn);
}
}
}
}
public Node1 removeNode(int d) {
// find the node
Node1 prev = findNode(d);
Node1 n = null;
/// if the node was found
if (prev != null) {
// set the found node to n
n = prev.getNext();
// set the one before to point to the one after
// ( skip over found node)
prev.setNext(n.getNext());
} else if (head.getData()== d){
n = head;
head = head.getNext();
n.setNext(null);
//return head
} // else return null
return n;
}
// return the previous node - this makes sense in a singly linked list
// because
// at least in the case of delete you have to be able to change the previous
// but even for any other use it is easy to go forward (just do getNext()
public Node1 findNode(int d) {
Node1 cur = head;
Node1 prev = head;
// while the current node is valid
while (cur != null) {
// check if the data we are looking for is in the current node
if (cur.getData() == d) {
// if it is return the previous so we can set the prev
return prev;
} else {
// otherwise go to the next node
prev = cur;
cur = cur.getNext();
}
}
// return null if not found
return null;
}
// This prints the list as it currently is
public void printList() {
if (head != null) {
Node1 cur = head;
while (cur != null) {
System.out.print(cur.getData() + " ");
cur = cur.getNext();
} // end of cycle through each node
System.out.println("");
} else { // if nothing in list
System.out.println("Empty List");
}
}
}
Node Class:
public class Node1 {
private int data;
private Node1 next ;
public Node1(int d){
data = d;
next = null;
}
public Node1(int d, Node1 n){
data = d;
next = n;
}
public int getData() {
return data;
}
public Node1 getNext() {
return next;
}
public void setData (int d){
data = d;
}
public void setNext (Node1 n){
next = n;
}
}
Explanation / Answer
LinkedListNode11 next; int value; public LinkedListNode1(int value) { this.value = value; this.next = null; } static int getValue(LinkedListNode1 node) { int value = node.value; while (node.next != null) { node = node.next; value = value * 10 + node.value; } return value; } static LinkedListNode1 getSum(LinkedListNode1 a, LinkedListNode1 b) { LinkedListNode1 answer = new LinkedListNode1(0); LinkedListNode1 ans = answer; int aval = getValue(a); int bval = getValue(b); int result = aval + bval; while (result > 0) { int len = (int) Math.pow((double) 10, (double) String.valueOf(result).length() - 1); int val = result / len; ans.next = new LinkedListNode1(val); ans = ans.next; result = result - val*len; } return answer.next; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.