Question

Complete Chapter 20 Programming Challenge 3. Modivy the LinkedList1 class by adding sort() and reverse(). sort()...

Complete Chapter 20 Programming Challenge 3. Modivy the LinkedList1 class by adding sort() and reverse(). sort() sorts the list in alphabetical order. reverse() reverses the order of the list. Make sure that you use recursion to implement these methods. no points will be given for non-recursive mehtods. Extend the GUI LinkedList1Demo class to support sort and reverse commands and use it to test the new methods.

Note: Both the LinkedList1 and the GUI LinkedList1Demo are available in the Source Code provided.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Since you have not provided the code of LinkedList1.java and LinkedList1Demo.java, i am providing the sort() and reverse() methods based on my understand. Feel free to reach out in case of any enquiries.

Sort()

node sortedMerge(node a, node b)

    {

        node result = null;

        /* Base cases */

        if (a == null)

            return b;

        if (b == null)

            return a;

  

        /* Pick either a or b, and recur */

        if (a.val <= b.val) {

            result = a;

            result.next = sortedMerge(a.next, b);

        }

        else {

            result = b;

            result.next = sortedMerge(a, b.next);

        }

        return result;

    }

  

    node mergeSort(node h)

    {

        // Base case : if head is null

        if (h == null || h.next == null) {

            return h;

        }

  

        // get the middle of the list

        node middle = getMiddle(h);

        node nextofmiddle = middle.next;

  

        // set the next of middle node to null

        middle.next = null;

  

        // Apply mergeSort on left list

        node left = mergeSort(h);

  

        // Apply mergeSort on right list

        node right = mergeSort(nextofmiddle);

  

        // Merge the left and right lists

        node sortedlist = sortedMerge(left, right);

        return sortedlist;

    }

  

    // Utility function to get the middle of the linked list

    node getMiddle(node h)

    {

        // Base case

        if (h == null)

            return h;

        node fastptr = h.next;

        node slowptr = h;

  

        // Move fastptr by two and slow ptr by one

        // Finally slowptr will point to middle node

        while (fastptr != null) {

            fastptr = fastptr.next;

            if (fastptr != null) {

                slowptr = slowptr.next;

                fastptr = fastptr.next;

            }

        }

        return slowptr;

    }

Reverse()

node reverseUtil(node curr, node prev) {

  

        /* If last node mark it head*/

        if (curr.next == null) {

            head = curr;

  

            /* Update next to prev node */

            curr.next = prev;

              

            return head;

        }

  

        /* Save curr->next node for recursive call */

node next1 = curr.next;

  

        /* and update next ..*/

        curr.next = prev;

  

        reverseUtil(next1, curr);

        return head;

    }

Add a comment
Know the answer?
Add Answer to:
Complete Chapter 20 Programming Challenge 3. Modivy the LinkedList1 class by adding sort() and reverse(). sort()...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • Instructions C++ programming Your task is to write a class called Data, stored in a file...

    Instructions C++ programming Your task is to write a class called Data, stored in a file named Data.h. Your class should be able to store a collection (vector) of integers. In addition to the appropriate constructors, your class should also have the following methods. void add (int number); Adds a number to the data set. void print (); Prints out the entire data set on a single line, separated by space. void sort (); Sorts the data set in ascending...

  • Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from...

    Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from a file that contain positive integers and should insert those numbers into the RB tree in that order. Note that the input file will only contain distinct integers. Print your tree by level using positive values for Black color and negative values for Red color Do not print out null nodes. Format for a node: <Node_value>, <Parent_value>). For example, the following tree is represented...

  • Interfaces 1. What is inside an interface definition? What does a class do to an interface...

    Interfaces 1. What is inside an interface definition? What does a class do to an interface and what keyword is involved? How does a class do this to an interface (what should we find in the class)? Can an interface have a generic parameter? How do you instantiate an interface as an object? What methods can’t you use on an interface type? Abstract Data Types 2. What does an ADT define? Does an ADT specify programming language and/or data structures...

  • Must be in Python 3 exercise_2.py # Define the Student class class Student():    # Ini...

    Must be in Python 3 exercise_2.py # Define the Student class class Student():    # Initialize the object properties def __init__(self, id, name, mark): # TODO # Print the object as an string def __str__(self): return ' - {}, {}, {}'.format(self.id, self.name, self.mark) # Check if the mark of the input student is greater than the student object # The output is either True or False def is_greater_than(self, another_student): # TODO # Sort the student_list # The output is the sorted...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Assignment: Chapter 18 introduces you to linked lists. In this homework assignment you will use the...

    Assignment: Chapter 18 introduces you to linked lists. In this homework assignment you will use the algorithms presented in this chapter to implement a list of names that should be kept in alphabetical order. Section 18.2 defines the Linked List Operations based on a list of numbers. You will change this code to work with a list of names. struct ListNode { string firstNname; string lastName; struct ListNode *next; }; Rename class NumberList to something more appropriate like StringList or...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT