Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have everything from a ----> h implemented I just need the i) Method and the j

ID: 3902432 • Letter: I

Question

I have everything from a ----> h implemented I just need the i) Method and the j) Method to be implemented:

I am attaching the whole code here, and the appendSortedASC(ListNode head, int val): // add to the LL in ASCENDING order

(j) appendSortedDESC(ListNode head, int val): add items in DESCENDING order to the LL

Please let me know if you need more explanation

I also need that you please show me what the code do with an output sample using all the methods:

(You should write test code in the main function to test all your implemented functions and print the output)

6. Implement Linked List operations. You can not use existing List APIs. Please implement the belowing operations: (a) getSize(ListNode head): return the size of the linked list (b) isNull(ListNode head): return true if the linked list is NULL; else false (c) display(ListNode head): print the value of each node in the linked list in order (d) insert(ListNode head, int n, int val): insert a new node to the nth position, the value of the new node equals to val. (e) remove(ListNode head, int n): remove the nth node (f) removeVal(ListNode head, int val): remove all the nodes that have value equal to val. (g) reverse(ListNode head): reverse the linked list (h) append(ListNode head, int val): creates a new node with the integer value to the end of the linked list (i) appendSortedASC(ListNode head, int val): creates a new node with the integer value and inserts it to the linked list in a location to maintain the ascending order. If the list is not already sorted, new node can be inserted anywhere. (j) appendSortedDESC(ListNode head, int val): creates a new node with the integer value and inserts it to the linked list in a location to maintain the descending order. If the list is not already sorted, new node can be inserted anywhere.

Explanation / Answer

import java.util.Random; class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } public class LinkedListOperations { public int getSize(ListNode head)// a) return the size of the linkedList { int size = 0; while (head != null) { size += 1; head = head.next; } return size; } public boolean isNull(ListNode head) { // b) return TRUE if the linkedList is NULL, else FALSE if (head == null) { return false; } return true; } public void display(ListNode head) { // c) print the value of each node in the LL in order if (head == null) { System.out.println(""); return; } int n = getSize(head); while (head != null) { if (n-- != 1) { System.out.print(head.val + "->"); head = head.next; } else { System.out.println(head.val); head = head.next; } } } public void insert(ListNode head, int n, int val) // d) insert a new node to the nth position, the value of the { // the new node equals to val ListNode tmp = new ListNode(val), p; int current = 0; while (current != n && head.next != null) { head = head.next; current++; } if (head.next == null) head.next = tmp; else { p = head.next; head.next = tmp; tmp.next = p; } return; } public ListNode remove(ListNode head, int n) { // remove the nth node if (head == null) return null; ListNode p = head; // get length of list int len = 0; while (p != null) { len++; p = p.next; } int fromStart = n; // if remove first node if (fromStart == 1) return head.next; p = head; // remove non-first node int i = 0; while (p != null) { i++; if (i == fromStart - 1) { p.next = p.next.next; } p = p.next; } return head; } public ListNode removeVal(ListNode head, int val) { // f)remove all the nodes that have value equal to val. ListNode helper = new ListNode(0); helper.next = head; ListNode p = helper; while (p.next != null) { if (p.next.val == val) { ListNode next = p.next; p.next = next.next; } else { p = p.next; } } return helper.next; } public ListNode reverse(ListNode head) { // g) reverse the linkedList if (head == null) return null; ListNode current = head; ListNode prev = null; ListNode next = null; while (current != null) { next = current.next; current.next = prev; prev = current; current = next; } head = prev; return head; } public void append(ListNode head, int val)// h) creates a new node with the integer value to the // end of the linked list { ListNode tmp = new ListNode(val); while (head.next != null) head = head.next; head.next = tmp; return; } public void appendSortedASC(ListNode head, int val) { ListNode tmp = new ListNode(val); if (head.val >= val) { ListNode headcopy = new ListNode(head.val); headcopy.next = head.next; head.next = headcopy; headcopy = head.next; head.val = val; return; } while (head.next != null && head.next.val
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote