Question: java the linked list code is import java.util.NoSuchElementException; import java.util.Iterator; public interface ReadonlyList { T get(int index); int size(); Iterator iterator(); } public class LinkedList

 

java

the linked list code is

import java.util.NoSuchElementException; 
import java.util.Iterator; public interface ReadonlyList { T get(int index); int size(); Iterator iterator(); }
 public class LinkedList implements ReadonlyList { private ListNode header = new ListNode(); public boolean isEmpty() { return header.getNext() == null; } public T getFirst() { ListNode first = header.getNext(); if (first == null) { throw new NoSuchElementException("List is empty, no item to get!"); } else { return first.getValue(); } } public void addFirst(T value) { // Create a new node ListNode newNode = new ListNode(); // Store the new value in the new node newNode.setValue(value); // Link the new node to the old first node newNode.setNext(header.getNext()); // Set the new node as the first node header.setNext(newNode); } public void removeFirst() { ListNode first = header.getNext(); if (first == null) { throw new NoSuchElementException("List is empty, no item to remove!"); } else { header.setNext(first.getNext()); } } public T get(int index) { int k = 0; ListNode current = header.getNext(); while (k  current = header.getNext(); while (current != null) { k++; current = current.getNext(); } return k; } public String toString() { ListNode current = header.getNext(); if (current == null) { return "[]"; } else { StringBuilder stringBuilder = new StringBuilder("["); stringBuilder.append(current.getValue()); while(current.getNext() != null) { stringBuilder.append(", "); current = current.getNext(); stringBuilder.append(current.getValue()); } stringBuilder.append("]"); return stringBuilder.toString(); } } public LinkedListIterator iterator() { return new LinkedListIterator(header); } }

 java the linked list code is import java.util.NoSuchElementException; import java.util.Iterator; public

2. [20 points] Write an implementation of bubble sort as an instance method called sort for the LinkedList class developed in lecture. sort ) should take a Comparator as a parameter. You may not use arrays in your implementation Hint 1: The simplest solution to this question will not involve changing the structure of the linked list. That is, the values in the linked list may change, but the links between nodes will not change. Hint 2: You may find it helpful to refer to the following alternative implementation of bubble sort over arrays as a reference for your implementation over linked lists public static void bubbleSort (Tl] a, Comparator c) boolean swapped true: int limit - a.length while (1imit 1 && swapped) swapped-false,; while (j + 1 != limit) if (c.compare (alj+1, a j)0) swapped-true T temp alj 11 a[j] = temp ; j++i limit

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!