Untitled

 avatar
unknown
plain_text
2 years ago
1.4 kB
8
Indexable
void printFromStart(Node head){ if(head == NULL) return; System.out.println(head.type.value); //firstly printing the node's type's value printFromStart(head.next); //calling recursively for next node of the linked list }

b) In this we have to print data from end so, in the recursion we move to the next node.until we reach the end and as we reach end we will start to print the nodes.

void printFromEnd(Node head){ if(head == NULL) return; printFromStart(head.next); //calling recursively for next node of the linked list System.out.println(head.type.value); //printing at the last }

c) In this we have to calculate sum of the node's data.
So, as we are printitng the node similiarly we will add the node's data along with the value return by the sub problem.

int sumOfMoney(Node head){ if(head == NULL) return; int sum = head.data; sum += printFromStart(head.next); return sum; }

d) In this we are going to use the same approach as previously but the difference will be that we will add data only when there is a node of Quarter type. for this we will use instanceof keyword.

int sumOfQuarters(Node head){ if(head == NULL) return; int sum = 0; if(head.type instanceof Quarters) sum += head.type.value; sum += printFromStart(head.next); return sum; }

So, this was the solutions of the problem.
I hope I am able to solve your problem, if yes then do give it a like.
It really helps :)

Editor is loading...