Question

Redo the insertionsort algorithm so that the values are inserted into a linked list rather than...

Redo the insertionsort algorithm so that the values are inserted into a linked list rather than an array. This eliminates the need to move other values when a new value is inserted, since your algorithm can simply insert a new node where the new value should go. Analyze your algo- rithm to obtain a big-O expression of the worst-case running time. Code your algorithm to produce a complete C++ program that reads in a list of 10 in- tegers, sorts the integers, and then writes out the sorted list. Your program should allow the user to repeat the process and sort another list until the user says he or she wants to exit the program. code in c++

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

//C++ program

#include<iostream>
using namespace std;

class LinkedList{
   private:
       typedef struct node{
           int data;
           struct node*next;
       }node;
       node *head;
      
   public:
       LinkedList(){
           head = NULL;
       }
       ~LinkedList(){
           node*temp = head;
           node*next;
           while(temp){
               next = temp->next;
               delete(temp);
               temp = next;
           }
           head = NULL;
       }
       void append(int data){
           node *newNode = new node;
           newNode->data = data;
           newNode->next = NULL;
          
           if(head==NULL){
               head = newNode;
               return;
           }
           node* temp = head;
          
           while(temp->next){
               temp = temp->next;
           }
           temp->next = newNode;
       }
      
       void print(){
           node*temp = head;
           while(temp){
               cout<<temp->data<<" ";
               temp = temp->next;
           }
           cout<<"\n\n";
       }
      
       void sortedInsert(node** head_ref, node* new_node)
       {
       node* current;
       if (*head_ref == NULL || (*head_ref)->data >= new_node->data)
       {
       new_node->next = *head_ref;
       *head_ref = new_node;
       }
   else
   {
   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;
   }
   }
      
       void insertionSort()
       {
   node *sorted = NULL;
       node *current = head;
       while (current != NULL)
       {
       node *next = current->next;
       sortedInsert(&sorted, current);
  
       current = next;
       }
       head = sorted;
       }
      
       void deleteList()
       {
           node* current = head;
           node* next;
  
           while (current != NULL)
           {
           next = current->next;
           delete(current);
           current = next;
           }
           head = NULL;
       }
      
};

int main(){
   LinkedList ll;
   int num,data;
   char choice;
   do{
       cout<<"Enter number of elements want to enter : ";
       cin>>num;
       while(num--){
           cout<<"Enter element : ";
           cin>>data;
           ll.append(data);
          
       }
       cout<<"Your unsorted Linked List\n";
       ll.print();
       ll.insertionSort();
       cout<<"Sorted Linked List\n";
       ll.print();
       ll.deleteList();
       cout<<"want to continue(y/n) : ";
       cin>>choice;
   }while(choice=='y'||choice=='Y');
   return 0;
}

//sample output

Add a comment
Know the answer?
Add Answer to:
Redo the insertionsort algorithm so that the values are inserted into a linked list rather than...
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
  • Write a Python function to implement the quick sort algorithm over a singly linked list. The...

    Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

  • 16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below,...

    16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below, write expressions that do the following, assuming that currPtr is somewhere in the middle of the list: a. Access the component member of the first list element. b. Advance currPtr to point to the next element. c. Access the component member of the next element (the one that follows the current element). d. Access the component member of the element that follows the next...

  • Create a linked list of at least 15 different values that are prompted for and entered...

    Create a linked list of at least 15 different values that are prompted for and entered while the program is running. Appending a node attaches that node to the end of the list while inserting a node places it in order maintaining a sorted list. Create a menu driven program where you have the options to appended, insert, display, and delete a node. Display the list after first appending data showing it in no specific order, delete all nodes and...

  • 5.8 Sort Linked List Use mergersort to sort a linked list Do not use the following...

    5.8 Sort Linked List Use mergersort to sort a linked list Do not use the following libraries: algorithm, cmath. Do not load the values into a vector and sort them. The sort must be done with a linked list Example Two lists 6->3->1->2->4->5 will return a list with 1->2->3->4->5->6. Input There will be no input read in anymore. Going forward use the hard-coded input on the template and ensure the program works. There will be one compare output test based...

  • Write a java program implementing the Linked list. It should be on an small office who...

    Write a java program implementing the Linked list. It should be on an small office who has 5 employees. The program ask the user for ID, First name, Last name and what field the work in(eg: accounting, programmer, HR etc). Each employee (with all the information of that paticular employee) should be placed in one node in the program. The program should repeat and ask the user for all 5 employees information. Also when you display the Linked list it...

  • #1. Single linked list - find... Extra info/hint? It's free For this problem, I have a...

    #1. Single linked list - find... Extra info/hint? It's free For this problem, I have a complete linked list program which supports the following commands from the user: insert, display, delete, average, find, insertBefore, insertAfter. insert allows them to insert a number into the current list, display will display the numbers in the current list etc. The program is complete except that I have removed the body of the method for find. Given the following class for the nodes in...

  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

  • (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...

    (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • Trying to figure out what needs to be in the headers.h file. I have written the...

    Trying to figure out what needs to be in the headers.h file. I have written the print function. Qualities in header file main() needs insertionSort() and mergeSortWrapper() Both insertionSort() and mergeSortWrapper() need print(). Please copy-and-paste the following files (0 Points): insertionSort.c /*--------------------------------------------------------------------------* *---- ----* *---- insertionSort.c ----* *---- ----* *---- This file defines a function that implements insertion ----* *---- sort on a linked-list of integers. ----* *---- ----* *---- ---- ---- ---- ---- ---- ---- ---- ---- ----* *----...

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