Answer to Question #241209 in C++ for Myname

Question #241209
. Due to a rush at the end of the exam, Professor could not arrange the answer sheets in the
sequence as desired. However, he mentioned the correct location of each answer sheet with it.
Now, Professor wants all the answer sheets in the desired sequence.
1
Expert's answer
2021-09-25T03:03:47-0400
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
    int data;
    Node* next;
};
void sortInsert(Node** head_ref,
                  Node* new_node)
{
    Node* curr;
    if (*head_ref == NULL
        || (*head_ref)->data
               >= new_node->data) {
        new_node->next = *head_ref;
        *head_ref = new_node;
    }
    else {
        curr = *head_ref;
        while (curr->next != NULL
&& curr->next->data
< new_node->data) {
            curr = curr->next;
        }
        new_node->next = curr->next;
        curr->next = new_node;
    }
}
 
Node* newNode(int new_data)
{
    
    Node* new_node = new Node();
 
    new_node->data = new_data;
    new_node->next = NULL;
 
    return new_node;
}
 
void displayList(Node* head)
{
    Node* temp = head;
    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }
}
int main()
{


    Node* head = NULL;
    Node* newN = newNode(5);
    sortInsert(&head, newN);
    newN = newNode(20);
    sortInsert(&head, newN);
    newN = newNode(17);
    sortInsert(&head, newN);
    newN = newNode(13);
    sortInsert(&head, newN);
    newN = newNode(11);
    sortInsert(&head, newN);
    newN = newNode(19);
    sortInsert(&head, newN);
    cout << "Created Linked List\n";
    displayList(head);
 
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS