### Need help with insertion sort for a C++ linked list. Should be a simple fix

Basically, I have to sort the linked list using insertion sort. I'm not very good at understanding the syntax behind the sort methods and feel completely lost with this list. I would really appreciate any help. I've done a ton of Googling but haven't had much luck finding out how to solve this. My instructor posted exactly what needs to be put on what line, but I'm still having trouble as I don't know what to do.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145`` ``````#include using namespace std; // Using some code from the book - see Chapter 19 Programs Number List class NumberList { private: struct ListNode { double value; struct ListNode *next; }; ListNode *head; public: /*Notice that this constructor is different than the one in the book. When a new linked list is made, a head node (which is given the value 0) is created and the head pointer will point to it. This head node should not be considered part of the data, it is just there to make the sorting easier. */ NumberList() { head = new ListNode; head->value = 0; head->next = NULL; } void appendNode(double); void InsertionSort(); void displayList() const; }; void NumberList::appendNode(double num) { //Just copy and paste the code for this function from the book. //Don't add anything new ListNode *newNode, *nodePtr; // Allocate a new node & store num newNode = new ListNode; newNode->value = num; newNode->next = NULL; // If there are no nodes in the list // make newNode the first node if (!head) head = newNode; else // Otherwise, insert newNode at end { // Initialize nodePtr to head of list nodePtr = head; // Find the last node in the list while (nodePtr->next) nodePtr = nodePtr->next; // Insert newNode as the last node nodePtr->next = newNode; } } void NumberList::displayList() const { //Just copy and paste the code for this function from the book. //Don't add anything new ListNode *nodePtr; nodePtr = head; while (nodePtr) { cout << nodePtr->value << endl; nodePtr = nodePtr->next; } } void NumberList::InsertionSort() { /* You can only use the 4 variables listed below. Dont' create any other variables*/ /*Don't add any more code than specified by the comments*/ NumberList temp; ListNode *add; ListNode *addNext; ListNode *insert; add = head->next; //The first node to be sorted is the one after the head node (which has the value 0) while (/*stop when there is nothing left to sort in the original list*/) { // Replace this comment with one line of code to assign a value to addNext //Replace this comment with one line of code to assign a value to insert while(/*stop when you are at the end of the temp list*/) { if(/* use the > operator to determine when to break out of this inner while loop*/) break; // Replace this comment with one line of code to assign a value to insert } // Replace this comment with one line of code to assign a value to add->next // Replace this comment with one line of code to assign a value to insert->next // Replace this comment with one line of code to assign a value to add } delete head; // Replace this comment with one line of code to assign a value to head temp.head =NULL; } int main() { NumberList example; example.appendNode(5); example.appendNode(2); example.appendNode(4); example.appendNode(7); example.appendNode(3); example.appendNode(1); example.appendNode(6); cout <<"In original order: "<
Last edited on
Topic archived. No new replies allowed.