Very very urgent. please don\'t post fake solution. A) 1) a) create a method cal
ID: 3560127 • Letter: V
Question
Very very urgent. please don't post fake solution.
A)
1)
a) create a method called sum in class Node , 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 LinkedList, 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.
2)
a) create a method called printBackwards in the same Node class. The method should take as a parameter an object of type PrintWriter. It should be a recursive method. The purpose of the method is to print all of the values from all of the nodes from the current node (as in the this keyword) until the end of the list IN BACKWARDS ORDER. In other words, the last node in the list will appear at the beginning of the output text file, and the current node will appear later in the text file. (like if the parameter passed to the method in class LinkedList is 2, for example, the output should be 5 4 3 2)
b) create a method printB in the same LinkedList class. The method should create an instance of PrintWriter and call the method printBackwards from class Node.
MAKE SURE that you use different output files for part 1 and part 2.
B) Using class Shape that you created from assignment 5. Change class Shape to implent the interface Comparable. The Shape objects should be compared based on their areas. Make sure to use the Generic Comparable.
C) Create a main class called Assignment 7. The main method should read from the file (input.txt) that is given below. The main method should create a new LinkedList Object, using the number 6 as the value passed to the constructor. Then the main method should read from the file and for each numeric value read it should add that number to the list. Then the method should call methods sum and printB of class LinkedList.
input.txt
Explanation / Answer
#include #include using namespace std; template class Node { public: T data; Node * next; Node(const T& d):data(d), next() {} Node(const Node& copyNode) : data(copyNode.data), next() {} private: Node& operator=(const Node&); }; template class LinkedList { public: Node * head; Node * tail; LinkedList(const LinkedList& LL); LinkedList& operator=(LinkedList byValList); LinkedList(): head(NULL), tail(NULL) {} LinkedList(Node * newNode) : head(newNode), tail(newNode) {} ~LinkedList(); static LinkedList sumLists(const LinkedList& LL1, LinkedList& LL2); void insertToTail(T val); void insertToHead(T val); void print(); void printBackwards(); }; template LinkedList::LinkedList(const LinkedList& LL) : head(NULL), tail(NULL) { const Node * curr = LL.head; if (!head && curr) { head = new Node(curr->data); tail = head; curr = curr->next; } while (curr) { Node * newNode = new Node(curr->data); tail->next = newNode; tail = newNode; curr = curr->next; } } template LinkedList& LinkedList::operator=(LinkedList byValList) { std::swap(head, byValList.head); return *this; } template LinkedList::~LinkedList() { Node * curr = head; while (head) { head = head->next; delete curr; curr = head; } } template void LinkedList::insertToTail(T val) { Node * newNode = new Node(val); if (tail == NULL) { newNode->next = tail; tail = newNode; head = newNode; return; } tail->next = newNode; tail = tail->next; } template void LinkedList::insertToHead(T val) { Node * newNode = new Node(val); newNode->next = head; head = newNode; if (head->next == NULL) tail = newNode; } template void LinkedList::print() { Node * curr = head; while (curr) { coutdata + carry) / 10; ResultList.insertToTail(newData); curr1 = curr1->next; curr2 = curr2->next; } while (curr1 || curr2) { if (carry) { if (curr1) ResultList.insertToTail(curr1->data + carry); if (curr2) ResultList.insertToTail(curr2->data + carry); carry = 0; continue; } if (curr1) { ResultList.insertToTail(curr1->data); curr1 = curr1->next; } if (curr2) { ResultList.insertToTail(curr2->data + carry); curr2 = curr2->next; } } return ResultList; } int main() { LinkedList LL1(new Node(7)); LL1.insertToTail(1); LL1.insertToTail(6); LL1.insertToTail(5); LL1.insertToTail(4); LinkedList LL2(new Node(5)); LL2.insertToTail(9); LL2.insertToTail(2); LinkedList LL = LL1.sumLists(LL1, LL2); LL.print(); LL2.print(); LL = LL2; LL.print(); LL2.print(); return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.