Question
If possible I would prefer the solution in Java.
Project 2: Implementation of Recursive Print and Merge List Functions for Singly Linked List Due: September 27, 1 PM Note: You should use the C++/Java source code attached to the project description for answering these questions Question 1- SinglyLinkedListRecursivePrint Project2 Question 1.cpp Questns 2 and 3 SinglyLinkedList_MergeLists_Project2_Question 23.cpp SinglyLinkedListRecursivePrint Project2 Question 1.java SinglyLinkedList MergeLists_Project2 Question 2_3.java Q1 -40 pts) Consider the implementation of the List ADT using Singly Linked List. Add a member function (to the List class) called recursivePrintForwardReverseOrders that prints the contents of the list in recursive fashion in both the forward order and reverse order For example, if the contents of the List are: 10-->4->812-->9, the recursive member function should print the List as follows: 10 4 8 12 9 9 12 8 4 0 Note that both the forward and reverse orders should be printed through an invocation of the recursive PrintForwardReverseOrders member function on the List object called from the main function. You are free to choose the paramcter(s) that need to be passed to the recursivePrintForwardReverseOrders function. But, you are not supposed to pass more than three parameter s). A suggestion for the parameter to pass is given in the main function of the code posted for stion To test your code (and take screenshot), create a List of at least 5 elements and then call the recursive PrintForwardReverseOrders function on this List object by passing a pointer to the first node in the Linked List as an argument, as shown in the main function of the Singly Linked List code for Question 1 Q2-25 pts) Add a member function mergeList to the Singly Linked List-based implementation of the List ADT. The member function takes as input a List object (representing the list of smaller size) as parameter and appends it to the List object (representing the list of larger size) on which the function wil be called. Consider largerIntegerList and smallerIntegerList to be the two List objects in the main function. The main function should call largerIntegerList.mergeListsmallerIntegerList) and then print the contents of the largerIntegerList using the IterativePrin) member function by calling largerIntegerList.IterativePrint) from main. The mergeList member function will append the elements of the smallerIntegerList to the end of the largerIntegerList. Use the Singly Linked List code for Question 2 attached in this project description.
Explanation / Answer
class LinkedList
{
Node head; // head of list
/* Linked list Node*/
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
/* Function to print reverse of linked list */
void printReverse(Node head)
{
if (head == null) return;
// print list of head node
printReverse(head.next);
// After everything else is printed
System.out.print(head.data+" ");
}
/* Utility Functions */
/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
/*Drier function to test the above methods*/
public static void main(String args[])
{
// Let us create linked list 1->2->3->4
LinkedList llist = new LinkedList();
llist.push(4);
llist.push(3);
llist.push(2);
llist.push(1);
llist.printReverse(llist.head);
}
}