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

Make code for this linked list 2. Linked Lists. Assume we have a headed linked l

ID: 3570191 • Letter: M

Question

Make code for this linked list

2. Linked Lists.

    Assume we have a headed linked list of doubles sorted in order from smallest to largest.    Complete the static addToList() method below to place a new double in the proper place within the sorted list, allowing duplicates. Assume that the Node class given here is used for each node in the list.

    public class Node {

       public Node() {} // default constructor

       public Node(double n, Node link) { // constructor

              data = n;

              next = link; }

       public Node getNext() { // selector

              return next; }

       public double getData() { // selector

              return data; }

       public void setNext(Node link) { // mutator

              next = link; }

       public void setData(double n) { // mutator

              data = n; }

       private Node next;

       private double data;

    } Node class

      

    public static void addToList(double n, Node l) { // inserts n into sorted list l

Explanation / Answer

public static void addToList(double n, Node l){
temp = Node(n)
temp.setNext(l.head)
l.head = temp
}

generally this is the method to insert into sorted list

void sortedInsert(struct node** head_ref, struct node* new_node)

{

    struct node* current;

    /* Special case for the head end */

    if (*head_ref == NULL || (*head_ref)->data >= new_node->data)

    {

        new_node->next = *head_ref;

        *head_ref = new_node;

    }

    else

    {

        /* Locate the node before the point of insertion */

        current = *head_ref;

        while (current->next!=NULL &&

               current->next->data < new_node->data)

        {

            current = current->next;

        }

        new_node->next = current->next;

        current->next = new_node;

    }

}

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