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
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.
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 sortedAppendACS(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 <= val) head = head.next; tmp.next = head.next; head.next = tmp; return; } //Please implement other methods public static void main(String[] args){ ListNode l1 = new ListNode(2); ListNode l2 = new ListNode(4); ListNode l3 = new ListNode(6); ListNode l4 = new ListNode(7); ListNode l5 = new ListNode(8); l1.next = l2; l2.next = l3; l3.next = l4; l4.next = l5; LinkedListOperations l = new LinkedListOperations(); //l.append(l1, 12); //l.sortedAppendACS(l1, 5); l.display(l5); l.sortedAppendACS(l5, 4); l.display(l5); l.sortedAppendACS(l5, 3); l.sortedAppendACS(l5, 2); l.sortedAppendACS(l5, 1); l.display(l5); System.out.println("The size of LinkedList l1 is: " + l.getSize(l5)); //l.display(l5); //System.out.println("The size of LinkedList l5 is: " + l.getSize(l5)); //l.display(null); //System.out.println("The size of empty LinkedList is: " + l.getSize(null)); /* l.sortedAppend(l1, 6); l.sortedAppend(l1, 4); l.sortedAppend(l1, 8); l.sortedAppend(l1, 5); Random rand = new Random(); for (int i = 0; i<1000; i++) { int rnd = rand.nextInt(1000); l.sortedAppendACS(l1, rnd); //System.out.println(rnd); } l.display(l1); */ } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started