Complete the addSorted ( ) method in ListNode2.java, which takes a new string an
ID: 3652850 • Letter: C
Question
Complete the addSorted ( ) method in ListNode2.java, which takes a new string and adds it into the linked list while maintaining descending order of the items in the list. Note: descending, not ascending order Use the main( ) method as shown in Figure 3.1 to test the addSorted( ) method. public static void main(String args[]) { ListNode2 list = new ListNode2(); System.out.println("Creating a linked list using the addSorted( ) method ..."); list = list.addSorted("Don"); list = list.addSorted("Syd"); list = list.addSorted("Mary"); list = list.addSorted("Peter"); list = list.addSorted("April"); list = list.addSorted("Adam"); list = list.addSorted("Eve"); list = list.addSorted("April"); list.displayAllNodes(); list = findAndRemove (list, "Syd"); System.out.println("After removing "Syd" ..."); list.displayAllNodes(); } //mainExplanation / Answer
void addSorted(List list, String str) { list.add(str); Collections.sort(list, Collections.reverseOrder()); } // You can use Collections to sort it, in ascending or descending order.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.