Use Java Please! thank you Write a class that provides the following three metho
ID: 3838458 • Letter: U
Question
Use Java Please! thank you
Write a class that provides the following three methods: Iteratively reverse a list public static LList iterativeReverseList(LList list) that accepts a reference to a LList and returns a reference to another LList that contains the data of the original list in reverse order. Your method should be implemented using iteration. Recursively Reverse a list) public static LList recursiveReverseList(LList list) that accepts a reference to a LList and returns a reference to another LList that contains the data of the original list in reverse order. Your method should be implemented using recursion. Sort Using Array List public static ArrayList insertSort(ArrayList list) that accepts a reference to an ArrayLIst and returns another ArrayList that contains the data of the original list in ascending order. You can implement your method as insertion sort or selection sort. You cannot use the Java sort methods. Write a test class toExplanation / Answer
// print iterative
class LinkedListIterative {
Node head;
static class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
/* Function to reverse the linked list */
Node reverse(Node node) {
Node prev = null;
Node current = node;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}
public static void main(String[] args) {
LinkedListIterative list = new LinkedListIterative();
list.head = new Node(1);
list.head.next = new Node(2);
list.head.next.next = new Node(3);
list.head.next.next.next = new Node(4);
System.out.println("Linked list");
list.printList(list.head);
list.head = list.reverse(list.head);
System.out.println("");
System.out.println("Reversed linked list ");
list.printList(list.head);
}
}
////////////////==============================================================================
// print recursive
class LinkedListRecursive
{
Node head; // head of list
/* Linked list Node*/
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
void Reverse(Node head)
{
if (head == null) return;
// recursively print list of head node
Reverse(head.next);
System.out.print(head.data+", ");
}
/* prepare the list. */
public void add(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public static void main(String args[])
{
//create linked list
LinkedListRecursive llist = new LinkedListRecursive();
llist.add(4);
llist.add(3);
llist.add(2);
llist.add(1);
llist.Reverse(llist.head);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.